首页 > 解决方案 > Check if one column's values are contained within another column's in R dataframes and then add the contained column as a new column

问题描述

I have this dataframes

D1$document = Emily 1.1.1.1. D2$user_name = Emily

I wish to match whether D2 to D1, and if D2$user_name column value (by row) is contained within the D1$document value by row too, then add D2$user_name corresponding value found in a new column in D1 called also D1$user_name.

I have tried str_detect, match etc but I can't seem to get it right?

unigram_data$user_name <- unigram_data %>% mutate(user_name = ifelse(str_detect(as.character(unigram_data$document)
                                , as.character(review_corpus$user_name)), 
                                cbind(user_name = review_corpus$user_name),
                                NA
                                ))

Thank you!

标签: rstringdataframedplyr

解决方案


是的,如果没有可重现的最小示例,真的不可能给你确切的建议。但是,这是一个使用case_when和的一般示例str_detect

dplyr::starwars %>% 
  select(name,homeworld,species) %>% 
  mutate(IsHuman = str_detect(species,'Human')) %>% 
  mutate(HumanFromNaboo = case_when(
    species == 'Human' & IsHuman == T ~ T,
    T ~ F
  ))

推荐阅读