我有两个数据框:
df.1 <- data.frame(loc = c('A','B','C','C'), person = c(1,2,3,4), str = c("door / window / table", "window / table / toilet / vase ", "TV / remote / phone / window", "book / vase / car / chair"))
因此,
loc person str
1 A 1 door / window / table
2 B 2 window / table / toilet / vase
3 C 3 TV / remote / phone / window
4 C 4 book / vase / car / chair
和,
df.2 <- data.frame(loc = c('A','B','C'), str = c("book / chair / chair", " table / remote / vase ", "window"))
这使,
loc str
1 A book / chair / car
2 B table / remote / vase
3 C window
我想创建一个变量df.1$percentage
来计算由 loc编辑df.1$str
的元素的百分比,或者:df.2$str
loc person str percentage
1 A 1 door / window / table 0.00
2 B 2 window / table / toilet / vase 0.50
3 C 3 TV / remote / phone / window 0.25
4 C 4 book / vase / car / chair 0.00
(1
有 0/3,2
有 2/4 匹配,3
有 1/4,4
有 0/4)
谢谢!