[英]replace in one factor by another factor (inside dataframe)
I have two factors and they do not have the same number of levels, but I would like to use one factor to replace values in the other factor inside a data-frame based on the name and the order of the factors.我有两个因素,它们的级别数不同,但我想使用一个因素来根据因素的名称和顺序替换数据框中另一个因素中的值。
My data looks like this,我的数据看起来像这样,
x <- factor(c("one", "two", "three", "two", "three"))
y <- factor(c(NA, "foo", NA, "bar", NA))
(df <- data.frame(x, y))
x y
1 one <NA>
2 two foo
3 three <NA>
4 two bar
5 three <NA>
and this is where I would like to end up,这就是我想要结束的地方,
x y z
1 one <NA> one
2 two foo foo
3 three <NA> three
4 two bar bar
5 three <NA> three
Should I convert the factors to a character vector?我应该将因子转换为字符向量吗?
you could use levels(z) <- c(levels(y), levels(x))
so that z has the required levels, however the underlying integer values may not relate correctly.您可以使用levels(z) <- c(levels(y), levels(x))
以便 z 具有所需的级别,但是基础整数值可能无法正确关联。 You are probably better off assigning to z using as.character
and then converting to factor.您最好使用as.character
分配给 z,然后转换为因子。
eg例如
df$z <- as.factor( ifelse(is.na(df$y), as.character(df$x), as.character(df$y)) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.