【发布时间】:2021-07-05 18:10:56
【问题描述】:
我正在尝试在不使用 for 循环的情况下提取 R 中每行数据帧中的唯一值。
df <- data.frame(customer = c('joe','jane','john','mary'), fruit = c('orange, apple, orange', NA, 'apple', 'orange, orange'))
df
customer fruit
1 joe orange, apple, orange
2 jane <NA>
3 john apple
4 mary orange, orange
我想要的 fruit 列是:
'橙色,苹果',NA,'苹果','橙色'
customer fruit
1 joe orange, apple
2 jane <NA>
3 john apple
4 mary orange
我尝试了一些类似的东西
apply(df, 1, function(x) unique(unlist(str_split(x[, "fruit"], ", "))))
它不工作。
如何在数据框中的每一行中获取唯一值?
【问题讨论】:
标签: r