Dear list, I'm trying to concatenate the values of two columns but im not able to do it: i have a dataframe with the following two columns: X VAR1 VAR2 1 2 2 1 3 2 4 3 5 4 6 4 what i would like to obtain is: X VAR3 1 2 2 1 3 2 4 3 5 4 6 4 I try with paste but what I obtain is: X VAR3 1 NA2 2 1NA 3 2NA 4 NA3 5 NA4 6 4NA Thanks a lot!! [[alternative HTML version deleted]]
Hi r-help-bounces at r-project.org napsal dne 05.05.2010 11:46:33:> > Dear list, > I'm trying to concatenate the values of two columns but im not able todo it:> > i have a dataframe with the following two columns: > > X VAR1 VAR2 > 1 2 > 2 1 > 3 2 > 4 3 > 5 4 > 6 4 > > > what i would like to obtain is: > X VAR3 > 1 2 > 2 1 > 3 2 > 4 3 > 5 4 > 6 4 > > I try with paste but what I obtain is: > X VAR3 > > 1 NA2 > 2 1NA > > 3 2NA > > 4 NA3 > > 5 NA4 > > 6 4NAIf your variables are numeric and if there are no values in both columns i.e. when Var1 is NA Var 2 is number you can rowSums(data.frame[,c("VAR1","VAR2")], na.rm=T) Regards Petr> > Thanks a lot!! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
On 5/5/2010 6:00 AM, n.vialma at libero.it wrote:> Dear list, > I'm trying to concatenate the values of two columns but im not able to do it: > > i have a dataframe with the following two columns: > > X VAR1 VAR2 > 1 2 > 2 1 > 3 2 > 4 3 > 5 4 > 6 4 > > > what i would like to obtain is: > X VAR3 > 1 2 > 2 1 > 3 2 > 4 3 > 5 4 > 6 4 > > I try with paste but what I obtain is: > X VAR3 > > 1 NA2 > 2 1NA > > 3 2NA > > 4 NA3 > > 5 NA4 > > 6 4NA > > Thanks a lot!! > > [[alternative HTML version deleted]] > >Hi, You don't say what you want to do when both VAR1 and VAR2 have non-trivial values. Neither do you indicate what is in the cells that are blank in your example. Nonetheless, consider this code: > X <- data.frame() > X <- edit(X) > X VAR1 VAR2 1 NA 2 2 1 NA 3 2 NA 4 NA 3 5 NA 4 6 4 NA > VAR3 <- X$VAR1 > VAR3 [1] NA 1 2 NA NA 4 > VAR3[is.na(VAR3)] <- X$VAR2[!is.na(X$VAR2)] > VAR3 [1] 2 1 2 3 4 4 > X <- cbind(X,VAR3) > X VAR1 VAR2 VAR3 1 NA 2 2 2 1 NA 1 3 2 NA 2 4 NA 3 3 5 NA 4 4 6 4 NA 4 Q.E.D. Marsh Feldman
Dear n.vialma, Good question! Your columns are of type factor(). Watch out for strange things with coercion (and so much for the 3 minute reply)! In this solution, you need a pre-allocated vector to store the results, and your approach is different depending on the data type you want the resulting vector to be. Say your data.frame() is defined below: df<-data.frame(x=c(1:6), var1=c("",1,2,"","",4),var2=c(2,"","",3,4,"")) # without stringsAsFactors=F The coercion gets fishy if you try to jump to numeric() as.numeric(df$var1) # didn't work. as.numeric(factor()) seems to cause problems [1] 1 2 3 1 1 4 as.character(df$var1) # works [1] "" "1" "2" "" "" "4" as.numeric(as.character(df$var1)) # works [1] NA 1 2 NA NA 4 Starting with factors, and resulting in either character() or numeric() dfm<-vector("character", 6) #pre allocate index1<-df$var1!="" index2<-df$var2!="" dfm[index1]<-as.character(df$var1[index1]) dfm[index2]<-as.character(df$var2[index2]) dfm<-as.numeric(dfm) Starting with character data (stringAsFactors=F), coercion works better with these data. df<-data.frame(x=c(1:6), var1=c("",1,2,"","",4),var2=c(2,"","",3,4,""), stringsAsFactors=F) as.numeric(df$var1) # works [1] NA 1 2 NA NA 4 If the data are numeric, the empty character fields coerce NA, so instead, you test for is.na() df<-data.frame(x=c(1:6), var1=c(NA,1,2,NA,NA,4),var2=c(2,NA,NA,3,4,NA)) dfm<-vector("numeric",6) index1<-!is.na(df$var1) index2<-!is.na(df$var2) dfm[index1]<-df$var1[index1] dfm[index2]<-df$var2[index2] Sincerely, KeithC. -----Original Message----- From: n.vialma at libero.it [mailto:n.vialma at libero.it] Sent: Wednesday, May 05, 2010 3:47 AM To: r-help at r-project.org Subject: [R] concatenate values of two columns Dear list, I'm trying to concatenate the values of two columns but im not able to do it: i have a dataframe with the following two columns: X VAR1 VAR2 1 2 2 1 3 2 4 3 5 4 6 4 what i would like to obtain is: X VAR3 1 2 2 1 3 2 4 3 5 4 6 4 I try with paste but what I obtain is: X VAR3 1 NA2 2 1NA 3 2NA 4 NA3 5 NA4 6 4NA Thanks a lot!! [[alternative HTML version deleted]]