Hello,I have the following data | colA | colB | colC | colD | | NA | pumpkin | NA | Pumpkin | | Cassava | NA | NA | Cassava | | yam | NA | NA | yam | | NA | Cherry | NA | Cherry | | NA | NA | Pepper | Pepper | | NA | NA | Mango | Mango | | maize | NA | NA | maize | All I want to do is to combine the first three columns in order to obtain the fourth column. A way forward?will be greatly appreciated. [[alternative HTML version deleted]]
Hello, Try the following. dat <- read.table(text = " | colA | colB | colC | colD | | NA | pumpkin | NA | Pumpkin | | Cassava | NA | NA | Cassava | | yam | NA | NA | yam | | NA | Cherry | NA | Cherry | | NA | NA | Pepper | Pepper | | NA | NA | Mango | Mango | | maize | NA | NA | maize | ", header = TRUE, sep = "|", stringsAsFactors = FALSE, na.strings = " NA ") dat <- dat[, -c(1, 6)] dat1 <- dat[, -4] res <- apply(dat1, 1, function(x) x[!is.na(x)]) res And please post your data examples using ?dput, it's not the first time you post to R-Help. Hope this helps, Rui Barradas Em 21-11-2016 21:26, Olu Ola via R-help escreveu:> Hello,I have the following data > | colA | colB | colC | colD | > | NA | pumpkin | NA | Pumpkin | > | Cassava | NA | NA | Cassava | > | yam | NA | NA | yam | > | NA | Cherry | NA | Cherry | > | NA | NA | Pepper | Pepper | > | NA | NA | Mango | Mango | > | maize | NA | NA | maize | > > > All I want to do is to combine the first three columns in order to obtain the fourth column. > A way forward will be greatly appreciated. > > [[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. >
Hi Olu, If you always have only one non-NA value in the first three columns: veg_df<-data.frame(col1=c(NA,"cassava","yam",NA,NA,NA,"maize"), col2=c("pumpkin",NA,NA,"cherry",NA,NA,NA), col3=c(NA,NA,NA,NA,"pepper","mango",NA)) veg_df$col4<-apply(as.matrix(veg_df),1,function(x) x[!is.na(x)]) Jim On Tue, Nov 22, 2016 at 8:26 AM, Olu Ola via R-help <r-help at r-project.org> wrote:> Hello,I have the following data > | colA | colB | colC | colD | > | NA | pumpkin | NA | Pumpkin | > | Cassava | NA | NA | Cassava | > | yam | NA | NA | yam | > | NA | Cherry | NA | Cherry | > | NA | NA | Pepper | Pepper | > | NA | NA | Mango | Mango | > | maize | NA | NA | maize | > > > All I want to do is to combine the first three columns in order to obtain the fourth column. > A way forward will be greatly appreciated. > > [[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.
Something along the lines of tmpfun <- function(chvec) chvec[!is.na(chvec)] apply( mydata, 1, tmpfun) Assuming every row has only one non-NA entry. -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 11/21/16, 1:26 PM, "R-help on behalf of Olu Ola via R-help" <r-help-bounces at r-project.org on behalf of r-help at r-project.org> wrote:> Hello,I have the following data >| colA | colB | colC | colD | >| NA | pumpkin | NA | Pumpkin | >| Cassava | NA | NA | Cassava | >| yam | NA | NA | yam | >| NA | Cherry | NA | Cherry | >| NA | NA | Pepper | Pepper | >| NA | NA | Mango | Mango | >| maize | NA | NA | maize | > > >All I want to do is to combine the first three columns in order to obtain >the fourth column. >A way forward will be greatly appreciated. > > [[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.
Thank you Jim and Don. On Monday, November 21, 2016 4:54 PM, Jim Lemon <drjimlemon at gmail.com> wrote: Hi Olu, If you always have only one non-NA value in the first three columns: veg_df<-data.frame(col1=c(NA,"cassava","yam",NA,NA,NA,"maize"), col2=c("pumpkin",NA,NA,"cherry",NA,NA,NA), col3=c(NA,NA,NA,NA,"pepper","mango",NA)) veg_df$col4<-apply(as.matrix(veg_df),1,function(x) x[!is.na(x)]) Jim On Tue, Nov 22, 2016 at 8:26 AM, Olu Ola via R-help <r-help at r-project.org> wrote:>? Hello,I have the following data > | colA | colB | colC | colD | > | NA | pumpkin | NA | Pumpkin | > | Cassava | NA | NA | Cassava | > | yam | NA | NA | yam | > | NA | Cherry | NA | Cherry | > | NA | NA | Pepper | Pepper | > | NA | NA | Mango | Mango | > | maize | NA | NA | maize | > > > All I want to do is to combine the first three columns in order to obtain the fourth column. > A way forward will be greatly appreciated. > >? ? ? ? [[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.[[alternative HTML version deleted]]