Skip to content

using if else function to complete a column in data frame

3 messages · Cacique Samurai, PIKAL Petr, David Winsemius

#
Hello all!

I?m trying to complete the "movimento" column in dataframe based in
the values of "kmr" column in two sequential lines, as below:

data example (dput in the end of email):

         ID    kmr movimento
5    10.700 314.20        NA
1    10.700 278.74        NA
2    10.700 278.74        NA
3    10.700 278.74        NA
4    10.700 278.74        NA
494 100.700 269.94        NA
500 100.700 278.74        NA
499 100.700 314.20        NA
495 100.700 278.74        NA
498 100.700 278.74        NA
496 100.700 255.40        NA
497 100.700 255.10        NA

Once I have different IDs, I wrote this function:

move = function (x){

  for (j in x$ID){

    for (i in 2:length(x$kmr)-1){

      if (x$kmr[i+1]  < x$kmr[i]) {
        x$movimento[i+1] <- "jusante"
      } else if (x$kmr[i+1] > x$kmr[i]) {
        x$movimento[i+1] <- "montante"
      } else {
        x$movimento[i+1] <- "parado"
      }

    }

  }

  return (x)
}

Worked pretty well with just one ID, but with many IDs the function
didn?t detach different IDs.

         ID    kmr movimento
5    10.700 314.20      <NA>
1    10.700 278.74   jusante
2    10.700 278.74    parado
3    10.700 278.74    parado
4    10.700 278.74    parado
494 100.700 269.94   jusante <-- this should be <NA>
500 100.700 278.74  montante
499 100.700 314.20  montante
495 100.700 278.74   jusante
498 100.700 278.74    parado
496 100.700 255.40   jusante
497 100.700 255.10   jusante

I also tried remove the first If condition and pass this function
using lapply in the splitted original data-frame, but didn?t work as
well.

Some onde can help?

Thanks in advanced,

Raoni

structure(list(ID = c("10.700", "10.700", "10.700", "10.700",
"10.700", "100.700", "100.700", "100.700", "100.700", "100.700",
"100.700", "100.700"), kmr = c(314.2, 278.74, 278.74, 278.74,
278.74, 269.94, 278.74, 314.2, 278.74, 278.74, 255.4, 255.1),
    movimento = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA)), .Names = c("ID", "kmr", "movimento"), row.names = c(5L,
1L, 2L, 3L, 4L, 494L, 500L, 499L, 495L, 498L, 496L, 497L), class = "data.frame")
#
Hi

Split option seems to me the most effective and ifelse is in this case not necessary.

You can use this function to estimate levels

fff <- function(x) sign(diff(x))
test.s <- split(test, test$ID)
for (i in 1:length(test.s)) test.s[[i]]$movimento[-1] <- fff(test.s[[i]][,2])
test <- do.call(rbind, test.s)
test[,3] <- factor(test[,3], levels=c(-1, 0, 1), labels=c("jusante", "parado", "montante"))
test

To avoid extension of row names you can use
library (plyr)
test<-ldply (test.s, data.frame)

instead of do.call.

I tried assigning factor within for cycle

fff<- function(x) factor(sign(diff(x)), levels=c(-1, 0, 1), labels=c("jusante", "parado", "montante"))
test.s <- split(test, test$ID)
for (i in 1:length(test.s)) test.s[[i]]$movimento[-1] <- fff(test.s[[i]][,2])
test <- do.call(rbind, test.s)
test

but assigned in this case is not factor but numeric vector, which seems to me strange. Maybe somebody could explain this behaviour

Cheers
Petr
________________________________
Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m.
Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu.
Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat.
Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu.

V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?:
- vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu.
- a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou.
- trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech.
- odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
#
The inner loop was not "recognizing" (or more accurately you were not causing the code to account for the fact) that you wanted this to be done within values of ID. Each pass of the outer loop was doing the same process inside the inner loop.
David Winsemius
Alameda, CA, USA