我有一个产品的 Dataframe (df)
,例如(df$id)
,其中包含一个由逗号分隔的1位或多位数字的变量(df$cat)
,每个数字对应于一个特定的“类别”。(一个产品可以分配给多个类别)。
我想使用键(key)
将这些从数字更改为字符串
df <- data.frame(id=c("id1","id2","id3","id4","id5","id6","id7","id8"),
cat=c("0,2,6","0","2","2,6","4,6","6","6","6"))
> head(df)
id cat
1 id1 0,2,6
2 id2 0
3 id3 2
4 id4 2,6
5 id5 4,6
6 id6 6
key <- data.frame(cat=c("0","2","4","6"),
name=c("kitchen","bathroom","dining","hall"))
所以我最后会得到
df.d <- data.frame(id=c("id1","id2","id3","id4","id5","id6","id7","id8"),
cat=c("0,2,6","0","2","2,6","4,6","6","6","6"),
location=c("kitchen,bathroom,hall","kitchen","bathroom","bathroom,hall","dining,hall","hall","hall","hall"))
head(df.d)
id cat location
1 id1 0,2,6 kitchen,bathroom,hall
2 id2 0 kitchen
3 id3 2 bathroom
4 id4 2,6 bathroom,hall
5 id5 4,6 dining,hall
6 id6 6 hall
7 id7 6 hall
8 id8 6 hall
尝试使用dplyr的重新编码,但没有成功
1条答案
按热度按时间ni65a41a1#
一种方法: