r - 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!
解决方案
是的,如果没有可重现的最小示例,真的不可能给你确切的建议。但是,这是一个使用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
))
推荐阅读
- android - Rxjava2:Single 和 CompositeDisposable 的用法
- android - 无法更新当前位置 FusedLocationClient
- javascript - 有没有办法在语义 ui 上使用带有自定义验证的 promise
- jquery - 向上滑动前更改文本
- python - 基于 IQR 按组去除异常值
- javascript - 项目不可拖动
- python - 错误:找不到 Requirement.parse('rest_framework>=3.6.3') 的合适分布
- spring-boot - 找不到 oneToOne 关系的实体
- javascript - 在本机反应中使用 Scrollview 和 KeyboardAvoidingView 阻止文本输入的键盘
- ios - 设备上的 iOS 单元测试无法启动 - HealthKit 权利错误