On 5/5/2010 6:00 AM, n.via...@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

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

Reply via email to