假设我有一个键值对列表:
l <- list("A" = 10, "B" = 20, "C" = 30)
带有向量值和相应类型向量的数据框:
df <- data.frame (type=c("A","A","B","B","B","C"),value=c(1,2,3,4,5,6))
df
type value
1 A 1
2 A 2
3 B 3
4 B 4
5 B 5
6 C 6
我想根据列表中的类型值来划分这些值,这样我最终会得到一个如下所示的数据框:
df
type value newval
1 A 1 0.10
2 A 2 0.20
3 B 3 0.15
4 B 4 0.20
5 B 5 0.25
6 C 6 0.20
我怀疑这很容易,但谷歌让我失望了,我一直想拔出我的头发一段时间。在我更熟悉的python中,我可以迭代行并使用dict作为我的列表,但它不明显如何做到这一点,在R中也不合适。
答案 0 :(得分:1)
如果您考虑加入或合并的条款,它就会变得直截了当。
请注意,我认为值是数字,而不是你的例子中的字符。
我喜欢data.table
因此会显示使用该pacakge
library(data.table)
# df with value as numeric
df <- data.frame (type=c("A","A","B","B","B","C"),value=1:6)
# create the data.table
DT <- data.table(df, key = 'type')
# create the key-value list as a data.table (specifying the levels the same
# as in DT[,type]
byl <- data.table(type = factor(names(l), levels = levels(DT[,type])), value = unlist(l), key = 'type')
# note they are both keyed by type, so we can join by type and then
# create a column that is value/ (value in the i component)
# so we use value / i.value
# i.value references value from the i argument (byl in this case)
DT[byl, newval := value / i.value ]
# look in DT now
DT
type value newval
1: A 1 0.10
2: A 2 0.20
3: B 3 0.15
4: B 4 0.20
5: B 5 0.25
6: C 6 0.20