检查 R 中嵌套列表中的文本

问题描述 投票:0回答:1

我有一个嵌套数据框,其中一列(评论)是一个包含列表(文本、评级、日期)的列表,如下所示的文本格式。

structure(list(Name = c("Afsondering Clinic", "The Local Choice Pharmacy Bergview"
), Reviews = list(structure(list(review_text = c("No Review Text", 
"No Review Text", "Given the poor standard of living in Eastern Cape - S.A not to mention the inefficiency in public sectors. This clinic truly thrives for excellence but must say: there is forever no medicine nor pills. Wonderful staff indeed, very helpful regardless of the state of affairs in the Makhoba village. Because of NO water NOR electricity, toilets don't flash etc 🙈. No play area for kids. Vaccines are done here."
), review_rating = c(5L, 5L, 4L), review_date = c("2020-07-03 07:12:13 +00:00", 
"2019-07-03 07:12:13 +00:00", "2019-07-03 07:12:13 +00:00")), class = "data.frame", row.names = c(NA, 
3L)), structure(list(review_text = c("Excellent service", "Went to Bergview Pharmacy looking for Liquid chlorophyll, I asked the lady who’s at the till on your way out, she’s light in complexion,had braids,her makeup done. What a rude and uncouth human being...", 
"A little on the expensive side but in general the staff that work's there are experienced and quick to answer and helpful. I will definitely recommend this pharmacy to all people.", 
"No Review Text", "Quick attendence friendly good carring"), review_rating = c(5L, 
1L, 5L, 5L, 5L), review_date = c("2024-05-03 07:12:15 +00:00", 
"2024-01-03 07:12:15 +00:00", "2023-11-03 07:12:15 +00:00", "2022-07-03 07:12:15 +00:00", 
"2021-07-03 07:12:15 +00:00")), class = "data.frame", row.names = c(NA, 
11L)))), row.names = 1:2, class = "data.frame")

我想检查对于任何地方是否至少存在一个不是“无评论文本”的评论,然后过滤数据框以仅包含这些地方。我正在努力在不使用 sapply 的情况下访问这些“review_text”元素。我怎样才能直接访问这些内容以便进行检查? 这是我想在其中使用它的代码:

facilities_with_reviews <- review_data %>% 
  filter(!is.na(Information$rating)) %>%
  filter(path to review_text != "No Review Text")

PS 我尝试使用 plyr $ 语法和普通 [] 但我无法让它工作

r nested-lists
1个回答
0
投票

我认为相关的 tidyverse 技巧是使用带有 mutate 的行式分组,因为 data.frames 嵌套到给定数据的行级别中。 您可能会采取不同的路线并解除嵌套,但如果保留嵌套,您仍然可以提取相关信息并执行各种操作示例:

mutate(rowwise(some_data),
  reviews_df_unique_review_text_count = length(unique(Reviews$review_text)),
  reviews_count_after_exclude_no_review_text = length(setdiff(unique(Reviews$review_text), "No Review Text")),
  list_of_review_texts = list(setdiff(unique(Reviews$review_text), "No Review Text")),
  review_texts_pasted_together = paste(setdiff(unique(Reviews$review_text), "No Review Text"), collapse = "; ")
)
© www.soinside.com 2019 - 2024. All rights reserved.