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

David L Carlson dcarlson at tamu.edu
Mon Oct 24 17:23:52 CEST 2016


You really need to provide more information. The error message without the command that generated it and the structure of the data sets you are using is a bare minimum. A reproducible example is often necessary to debug code such as this.

You can simplify your code substantially with something like this

> cols <- c("H1", "H2", "H3", "H8")
> RS <- rowSums(kdata[, cols])
> kdata$target <- ifelse(RS == length(cols), "all-true",
+       ifelse(RS == 0, "all-false", "other"))

David L. Carlson
Department of Anthropology
Texas A&M University


-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Indhira, Anusha
Sent: Monday, October 24, 2016 7:06 AM
To: r-help at r-project.org
Subject: Re: [R] using nested ifelse when i want to process values from more than 3 columns of data frame

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.



More information about the R-help mailing list