[R] using nested ifelse when i want to process values from more than 3 columns of data frame

Mark Sharp msharp at txbiomed.org
Mon Oct 24 17:29:33 CEST 2016


Indira,

I think you are wanting to define a new column (target) with "all_true", "all_false", or "other" based on columns H1, H2, H3, and H8 all being equal to 1, all being equal to 0, and other wise respectively. If so, then you do not need apply() or ifelse().

I believe this does what you were trying to do. I am not persuaded you are doing what you need to do.

kdata$target <- rep("other", nrow(kdata))
kdata$target[kdata$H1 == 0 &
             kdata$H2 == 0 &
             kdata$H3 == 0 &
             kdata$H8 == 0] <- "all_false"
kdata$target[kdata$H1 == 1 &
             kdata$H2 == 1 &
             kdata$H3 == 1 &
             kdata$H8 == 1] <- "all_true"

Mark

R. Mark Sharp, Ph.D.
Director of Primate Records Database
Southwest National Primate Research Center
Texas Biomedical Research Institute
P.O. Box 760549
San Antonio, TX 78245-0549
Telephone: (210)258-9476
e-mail: msharp at TxBiomed.org








> On Oct 24, 2016, at 7:06 AM, Indhira, Anusha <Anusha.Indhira at controlsdata.com> wrote:
>
> Got past the syntax error,now I got  new error below
>
> Error in match.fun(FUN) : c("'ifelse((kmeans.data$HBV51OpenDmd & kmeans.data$HBV52OpenDmd & ' is not a function, character or symbol", "' kmeans.data$HBV53OpenDmd & kmeans.data$HBV8OpenDmd) == 1, ' is not a function, character or symbol", "' \"all-true\", (ifelse((kmeans.data$HBV51OpenDmd == 0 & kmeans.data$HBV52OpenDmd == ' is not a function, character or symbol", "' 0 & kmeans.data$HBV53OpenDmd == 0 & kmeans.data$HBV8OpenDmd == ' is not a function, character or symbol", "' 0), \"all-false\", \"other\")))' is not a function, character or symbol" )
>
>
> From: Indhira, Anusha
> Sent: 24 October 2016 12:56
> To: 'r-help at r-project.org'
> Subject: using nested ifelse when i want to process values from more than 3 columns of data frame
>
>
> Hi,
>
> I would like to process each row of data frame having 8 columns of which I am interested in 4 columns for my analysis.I tried using apply function but I get syntax error,although I checked for missing syntaxes ,couldn't figure out the mistake.Can you also suggest me more efficient way to perform same action.
>
> # creating target column from result
> kdata$target <- apply(kdata,1,ifelse((kdata$H1 & kdata$H2 & kdata$H3 &
>                                                         kdata$H8) == 1,"all-true",(ifelse(kdata$H1== 0 &
>                                                                                                            kdata$H2 == 0 &
>                                                                                                            kdata$H3 == 0 &
>                                                                                                            kdata$H8 == 0) ,
>                                                                                                   "all-false","other")))
> Thanks.
> Alily
> This e-mail (including attachments) contains contents owned by Rolls-Royce plc and its subsidiaries, affiliated companies or customers and covered by the laws of England and Wales, Brazil, US, or Canada (federal, state or provincial). The information is intended to be confidential and may be legally privileged. If you are not the intended recipient, you are hereby notified that any retention, dissemination, distribution, interception or copying of this communication is strictly prohibited and may subject you to further legal action. Reply to the sender if you received this email by accident, and then delete the email and any attachments.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

CONFIDENTIALITY NOTICE: This e-mail and any files and/or...{{dropped:10}}



More information about the R-help mailing list