[R] if else statements in data frame
Dear R experts, I have again a problem. Let I have a data frame like below: x y -- A A A B B B A B where x and y are the column names. I want to create a new column z where it is elements will be like that, if at each row has there at least one 'B', then z will have value of B, if not then Z will have missing value(NA) for the corresponding row. So my result data frame should be like below: x y Z -- A A NA A B B B B B A B B How can I do this with if else statements in R. Thanks a lot. -- View this message in context: http://r.789695.n4.nabble.com/if-else-statements-in-data-frame-tp3454646p3454646.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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.
[R] adding two colmns of a data frame
It seems very simple but I could not find the way. I just want to add two columns of a data frame. I am doing the below operation: data$C=transform(data, data$A+data$B) However that return as a data frame. I will be glad if ypu help. Thanks a lot -- View this message in context: http://r.789695.n4.nabble.com/adding-two-colmns-of-a-data-frame-tp3454556p3454556.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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.
Re: [R] adding two colmns of a data frame
Sorry, sorry for taking your time. I solved the problem. data=transform(data,C= A+B) It is so simple. But if you dont know it is hard. Again sory for taking time -- View this message in context: http://r.789695.n4.nabble.com/adding-two-colmns-of-a-data-frame-tp3454556p3454572.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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.
Re: [R] if else statements in data frame
Thanks for the answers. They were very helpful. I did it like below. data$z <- ifelse(data$x == "A" & data$y == "A", "NA", "B"). That is enough for today. I guess I learned a enough today. Thanks to everbody who tried to help. -- View this message in context: http://r.789695.n4.nabble.com/if-else-statements-in-data-frame-tp3454646p3454710.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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.
Re: [R] if else statements in data frame
Thanks alot B77S. That was a critical post. -- View this message in context: http://r.789695.n4.nabble.com/if-else-statements-in-data-frame-tp3454646p3455077.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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.