嗨,我有一个如下数据框:
beverage <- c('tea', 'tea', 'coffee', 'coffee', 'tea', 'tea', 'coffee', 'coffee')
milk <- c('FALSE', 'FALSE','FALSE', 'FALSE', 'TRUE', 'TRUE','TRUE', 'TRUE')
df1 <- data.frame(beverage, milk)
df1
beverage milk
1 tea FALSE
2 tea FALSE
3 coffee FALSE
4 coffee FALSE
5 tea TRUE
6 tea TRUE
7 coffee TRUE
8 coffee TRUE
我想添加另一列beverage_milk
,该列将获取饮料列的内容,如果牛奶列表示为_milk
,则添加TRUE
。像这样:
beverage milk beverage_milk
1 tea FALSE tea
2 tea FALSE tea
3 coffee FALSE coffee
4 coffee FALSE coffee
5 tea TRUE tea_milk
6 tea TRUE tea_milk
7 coffee TRUE coffee_milk
8 coffee TRUE coffee_milk
我一直在考虑使用类似的东西:
beverage_milk = within(beverage_milk{
beverage_milk = ifelse(milk == TRUE, beverage(+_milk), beverage})
很明显,我知道(+ _milk)将不起作用,但是这种方法目前不适用于我,非常感谢您的帮助。
答案 0 :(得分:2)
一个选项是replace
(比ifelse
快一点)
df1$milk <- as.logical(df1$milk)
df1$beverage_milk <- with(df1, replace(beverage, milk, paste(beverage, "milk", sep = "_")))
df1
# beverage milk beverage_milk
#1 tea FALSE tea
#2 tea FALSE tea
#3 coffee FALSE coffee
#4 coffee FALSE coffee
#5 tea TRUE tea_milk
#6 tea TRUE tea_milk
#7 coffee TRUE coffee_milk
#8 coffee TRUE coffee_milk
ifelse
的语法可能是
df1$beverage_milk <- with(df1, ifelse(milk, paste(beverage, "milk", sep = "_"), beverage))
数据
beverage <- c('tea', 'tea', 'coffee', 'coffee', 'tea', 'tea', 'coffee', 'coffee')
milk <- c('FALSE', 'FALSE','FALSE', 'FALSE', 'TRUE', 'TRUE','TRUE', 'TRUE')
df1 <- data.frame(beverage, milk, stringsAsFactors = FALSE)
答案 1 :(得分:0)
这是一个dplyr
选项
library(dplyr)
df1 %>%
mutate_if(is.factor, as.character) %>%
mutate(beverage_milk = if_else(milk == TRUE, paste0(beverage, "_milk"), beverage))
# beverage milk beverage_milk
#1 tea FALSE tea
#2 tea FALSE tea
#3 coffee FALSE coffee
#4 coffee FALSE coffee
#5 tea TRUE tea_milk
#6 tea TRUE tea_milk
#7 coffee TRUE coffee_milk
#8 coffee TRUE coffee_milk
或在基数R
中transform(df1, beverage_milk = ifelse(
milk == TRUE, paste0(beverage, "_milk"), as.character(beverage)))
# beverage milk beverage_milk
#1 tea FALSE tea
#2 tea FALSE tea
#3 coffee FALSE coffee
#4 coffee FALSE coffee
#5 tea TRUE tea_milk
#6 tea TRUE tea_milk
#7 coffee TRUE coffee_milk
#8 coffee TRUE coffee_milk
PS。您的样本数据不可复制; beverage
包含一个附加的错误元素。
答案 2 :(得分:0)
使用dplyr
可以通过以下方式获得解决方案:
library(dplyr)
beverage <- c('tea', 'tea', 'coffee', 'coffee', 'tea', 'tea', 'coffee', 'coffee')
milk <- c('FALSE', 'FALSE','FALSE', 'FALSE', 'TRUE', 'TRUE','TRUE', 'TRUE')
df1 <- data.frame(beverage, milk)
df1 %>%
mutate(beverage_milk = ifelse(milk == 'TRUE', paste0(beverage, "_milk"), paste(beverage)))
# beverage milk beverage_milk
#1 tea FALSE tea
#2 tea FALSE tea
#3 coffee FALSE coffee
#4 coffee FALSE coffee
#5 tea TRUE tea_milk
#6 tea TRUE tea_milk
#7 coffee TRUE coffee_milk
#8 coffee TRUE coffee_milk