Combine factorial column intp a new column

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Combine factorial column intp a new column

Beatriz
Dear R-users,

I'd be very greatful if you could help me with the following as after a
few tests I haven't still been able to get the right outcome.

I've got this data:
dd_1 <- data.frame(ID = c("1","2", "3", "4", "5"),
                  Class_a = c("a",NA, "a", NA, NA),
                  Class_b = c(NA, "b", "b", "b", "b"))

And I'd like to produce a new column 'CLASS':
dd_2 <- data.frame(ID = c("1","2", "3", "4", "5"),
                  Class_a = c("a",NA, "a", NA, NA),
                  Class_b = c(NA, "b", "b", "b", "b"),
                  CLASS = c("a", "b", "a-b", "b", "b"))

Thanks a lot!

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Combine factorial column intp a new column

arun kirshna


Hi,
May be this helps:
 ddNew <-  transform(dd_1, CLASS= gsub("NA-|-NA","",paste(Class_a, Class_b, sep="-")))
 identical(ddNew, dd_2)
#[1] TRUE
A.K.


On Thursday, May 22, 2014 4:07 AM, Beatriz R. Gonzalez Dominguez <[hidden email]> wrote:
Dear R-users,

I'd be very greatful if you could help me with the following as after a
few tests I haven't still been able to get the right outcome.

I've got this data:
dd_1 <- data.frame(ID = c("1","2", "3", "4", "5"),
                  Class_a = c("a",NA, "a", NA, NA),
                  Class_b = c(NA, "b", "b", "b", "b"))

And I'd like to produce a new column 'CLASS':
dd_2 <- data.frame(ID = c("1","2", "3", "4", "5"),
                  Class_a = c("a",NA, "a", NA, NA),
                  Class_b = c(NA, "b", "b", "b", "b"),
                  CLASS = c("a", "b", "a-b", "b", "b"))

Thanks a lot!

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.