假设我有以下数据框:
df <- data.frame(x = c(1,1,1,3,3,3,4,4,4),
y = c(1,3,4,1,3,4,1,3,4),
z = c(6,3,7,8,2,3,0,1,2))
我想根据另一个数据框中包含的映射重命名列x和y中的值:
df.aux <- data.frame(id = c(0,1,2,3,4),
desc = c('zero', 'one', 'two', 'three', 'four))
获得:
x y z
1 one one 6
2 one three 3
3 one four 7
4 three one 8
5 three three 2
6 three four 3
7 four one 0
8 four three 1
9 four four 2
请注意,df.aux中的映射可能包含比df更多的级别。我可以通过将列名称df $ x更改为df $ id来进行映射,合并两个df&#39;然后重复df $ y。但它非常笨拙,所以必须有更好的方法。
答案 0 :(得分:3)
您可以使用匹配功能进行映射:
df.new <- data.frame(x=df.aux$desc[match(df$x, df.aux$id)], y=df.aux$desc[match(df$y, df.aux$id)], z=df$z)
答案 1 :(得分:1)
factor
函数允许直接分配映射和标签。
> df <- data.frame(x = c(1,1,1,3,3,3,4,4,4),
+ y = c(1,3,4,1,3,4,1,3,4),
+ z = c(6,3,7,8,2,3,0,1,2))
> df.aux <- data.frame(id = c(0,1,2,3,4), desc = c('zero', 'one', 'two', 'three', 'four'))
> df$x <- factor(df$x, levels = df.aux$id, labels = df.aux$desc)
> df$y <- factor(df$y, levels = df.aux$id, labels = df.aux$desc)
> df
x y z
1 one one 6
2 one three 3
3 one four 7
4 three one 8
5 three three 2
6 three four 3
7 four one 0
8 four three 1
9 four four 2
如果df.aux
太大而无法快速完成,您可以先使用
df.aux[df.aux$id %in% union(df$x, df$y),]