r - How to fill a column based on a condition using sum() for matches in r
问题描述
I have struggles filling a column based on a condition. Maybe my approach is not in the right direction. I don't know. My conditions are as follow:
- 2 "b"s and 1 "a" in a row, write in column "match" "B"
- 2 "c"s in a row, write in column "match" "C"
- for anything else fill NA
So far I did the following but I see that this is not quite accurate since my new vector is not created from the rows but the entire column, and it still doesn't work.
set.seed(123)
df_letters <- data.frame(basket1 = sample(letters[1:3], 5, replace = TRUE, prob = c(0.85,0.10,0.5)),
basket2 = sample(letters[1:3], 5, replace = TRUE, prob = c(0.10,0.85,0.5)),
basket3 = sample(letters[1:3], 5, replace = TRUE, prob=c(0.5,0.10,0.85)),
stringsAsFactors = FALSE)
df_letters %>% mutate(match = ifelse(sum(as.character(as.vector(df_letters)) == "c")==2, "C",
ifelse((sum(as.character(as.vector(df_letters)) == "b")==2) & (sum(as.character(as.vector(df_letters)) == "a")==1) ,"B", NA )))
My desired output is:
> df_letters
basket1 basket2 basket3 match
1 a b b B
2 c b c C
3 a c a <NA>
4 c b c C
5 b b c <NA>
Many thanks in advance!
解决方案
一种dplyr
选择可能是:
df_letters %>%
mutate(match = case_when(rowSums(select(., starts_with("basket")) == "b") == 2 & rowSums(select(., starts_with("basket")) == "a") == 1 ~ "B",
rowSums(select(., starts_with("basket")) == "c") == 2 ~ "C",
TRUE ~ NA_character_))
basket1 basket2 basket3 match
1 a b b B
2 c b c C
3 a c a <NA>
4 c b c C
5 b b c <NA>
推荐阅读
- reactjs - 为什么 React 类组件转换为功能组件,其中钩子未在文档的事件处理程序中获取状态
- python-3.x - 将列添加到数据框并合并
- spring - Spring Batch 是否适合定期从数据库中删除记录
- json - 如何将响应 json 数据的值保存到 AsyncStorage?
- python - 嵌套的两个 OrderedDict 之间的比较
- android - 无法在未调用 Looper.prepare() 的线程内创建处理程序
- java - 如何在不接受输入的情况下修复正在运行的循环?
- c++ - 使用 sqrt() 时精度损失
- python-3.x - 如何在一个语句上修复两个条件
- python - 如何在python中使用beautifulsoup从网页中获取数据