以下是样本数据。
df1 <- data.frame(y = 1:5, x = c("s", "m", "l", "s", "m"))
df2 <- data.frame(y = 1:4, x = c("s", "l", "s", "l"))
我希望df2有三个级别,基于df1 $ x。
我试过
df2$x <- factor(df1$x)
然后我有了
Error in `$<-.data.frame`(`*tmp*`, "x", value = c(3L, 2L, 1L, 3L, 2L)) :
replacement has 5 rows, data has 4
或
levels(df2$x) <- factor(df1$x)
然后它改变了df2。
df2
y x
1 1 m
2 2 s
3 3 m
4 4 s
我该怎么做?
答案 0 :(得分:1)
我不确定我是否理解你的目标。
df1 <- data.frame(y = 1:5, x = factor(c("s", "m", "l", "s", "m")))
df2 <- data.frame(y = 1:4, x = factor(c("s", "l", "s", "l")))
df2$x
#[1] s l s l
#Levels: l s
levels(df2$x)<-unique(c(levels(df2$x),levels(df1$x)))
df2$x
#[1] s l s l
#Levels: l s m
答案 1 :(得分:0)
df1 <- data.frame(y = 1:5, x = c("s", "m", "l", "s", "m"))
df2 <- data.frame(y = 1:4, x = factor(c("s", "l", "s", "l"), levels=levels(df1$x)))
str(df2)
'data.frame': 4 obs. of 2 variables:
$ y: int 1 2 3 4
$ x: Factor w/ 3 levels "l","m","s": 3 1 3 1