[R] Odp: concatenate values of two columns

Petr PIKAL petr.pikal at precheza.cz
Wed May 5 12:11:07 CEST 2010


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 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


If 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 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