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

PIKAL Petr petr.pikal at precheza.cz
Mon Jan 9 08:35:22 CET 2017


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

> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Cacique
> Samurai
> Sent: Monday, January 9, 2017 6:22 AM
> To: R help <r-help at r-project.org>
> Subject: [R] using if else function to complete a column in data frame
>
> 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")
>
> --
> 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.

________________________________
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.


More information about the R-help mailing list