如果第三列为真,如何将列内容后缀到新列中R

时间:2019-02-21 21:34:41

标签: r

嗨,我有一个如下数据框:

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)将不起作用,但是这种方法目前不适用于我,非常感谢您的帮助。

3 个答案:

答案 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