Beatriz R. Gonzalez Dominguez
2014-May-22 08:05 UTC
[R] Combine factorial column intp a new column
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!
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
<aguitatierra at hotmail.com> 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!
______________________________________________
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.