[R] using if else function to complete a column in data frame

David Winsemius dwinsemius at comcast.net
Mon Jan 9 08:36:34 CET 2017


> On Jan 8, 2017, at 9:21 PM, Cacique Samurai <caciquesamurai at gmail.com> wrote:
> 
> 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>

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.


> 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")
> 
> -- 
> Raoni Rosa Rodrigues
> Research Associate of Fish Transposition Center CTPeixes
> Universidade Federal de Minas Gerais - UFMG
> Brasil
> rodrigues.raoni at gmail.com
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list