r - 计算列表中的值

标签 r list

我有一个列,其观察结果为字符类型,并按如下方式组织(输出示例如下):

df <- data.frame(observation = c('["Extra pillows and blankets", "Dishes and silverware", "Room-darkening shades", "Hot water kettle", "Ethernet connection", "Wifi", "Dedicated workspace", "Oven"]',
                                 '["Extra pillows and blankets", "Dishes and silverware", "Room-darkening shades", "Hot water kettle", "Ethernet connection", "Wifi", "Dedicated workspace", "Oven"]',
                                 '["Extra pillows and blankets", "Dishes and silverware", "Room-darkening shades", "Hot water kettle", "Ethernet connection", "Wifi", "Dedicated workspace", "Oven"]'
))

我的目标是计算每个观察的每个列表中的元素数量(考虑在列表中用逗号分隔这些元素)。 我尝试过将其转换为一个因子、一个列表,我使用了长度和长度,以及许多其他我什至不记得的东西。有谁知道如何解决这个问题吗?

最佳答案

我们可以使用str_count()对元素进行计数:

这里我们对进行计数,并加1来得到元素的数量:

library(dplyr)
library(stringr)

df %>%
  mutate(n_elements = str_count(observation, ",")+1)
1 ["Extra pillows and blankets", "Dishes and silverware", "Room-darkening shades", "Hot water kettle", "Ethernet connection", "Wifi", "Dedicated workspace", "Oven"]
2 ["Extra pillows and blankets", "Dishes and silverware", "Room-darkening shades", "Hot water kettle", "Ethernet connection", "Wifi", "Dedicated workspace", "Oven"]
3 ["Extra pillows and blankets", "Dishes and silverware", "Room-darkening shades", "Hot water kettle", "Ethernet connection", "Wifi", "Dedicated workspace", "Oven"]
  n_elements
1          8
2          8
3          8

关于r - 计算列表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76190404/

相关文章:

r - data.table 行引用行为

python - 将字符串转换为列表的正则表达式(Python)

Python:将随机数放入列表中

r - ggplot2 中标签文本条目中的不同字体和大小

c++ - R 矩阵到 Armadillo 的转换真的很慢

r - 使用 str_detect 进行特殊符号模式搜索

regex - 不丢失字符的拆分字符串

css - 显示 : inline not working

c# - 使用 lambda 从对象及其列表创建通用列表

python - 如何在 Python 中读取和删除文件中的前 n 行 - 优雅的解决方案