如何根据多列中的多个条件创建新列?

时间:2016-09-09 07:01:32

标签: r if-statement dataframe calculated-columns multiple-conditions

我正在尝试根据其他列的几个条件向数据框添加新列。我有以下数据:

> commute <- c("walk", "bike", "subway", "drive", "ferry", "walk", "bike", "subway", "drive", "ferry", "walk", "bike", "subway", "drive", "ferry")
> kids <- c("Yes", "Yes", "No", "No", "Yes", "Yes", "No", "No", "Yes", "Yes", "No", "No", "Yes", "No", "Yes")
> distance <- c(1, 12, 5, 25, 7, 2, "", 8, 19, 7, "", 4, 16, 12, 7)
> 
> df = data.frame(commute, kids, distance)
> df
   commute kids distance
1     walk  Yes        1
2     bike  Yes       12
3   subway   No        5
4    drive   No       25
5    ferry  Yes        7
6     walk  Yes        2
7     bike   No         
8   subway   No        8
9    drive  Yes       19
10   ferry  Yes        7
11    walk   No         
12    bike   No        4
13  subway  Yes       16
14   drive   No       12
15   ferry  Yes        7

如果满足以下三个条件:

commute = walk OR bike OR subway OR ferry
AND
kids = Yes
AND
distance is less than 10

然后我想要一个名为get.flyer的新列等于“是”。最终的数据框应如下所示:

   commute kids distance get.flyer
1     walk  Yes        1       Yes
2     bike  Yes       12       Yes
3   subway   No        5          
4    drive   No       25          
5    ferry  Yes        7       Yes
6     walk  Yes        2       Yes
7     bike   No                   
8   subway   No        8          
9    drive  Yes       19          
10   ferry  Yes        7       Yes
11    walk   No                   
12    bike   No        4          
13  subway  Yes       16       Yes
14   drive   No       12          
15   ferry  Yes        7       Yes

3 个答案:

答案 0 :(得分:10)

我们可以使用%in%来比较列中的多个元素&,以检查两个条件是否都为TRUE。

library(dplyr)
df %>%
     mutate(get.flyer = c("", "Yes")[(commute %in% c("walk", "bike", "subway", "ferry") & 
           as.character(kids) == "Yes" & 
           as.numeric(as.character(distance)) < 10)+1] )

最好使用data.frame创建stringsAsFactors=FALSE,默认情况下为TRUE。如果我们检查str(df),我们会发现所有列都是factor类。此外,如果缺少值,则可以使用""代替NA,以避免将class列的numeric转换为其他内容。

如果我们重写&#39; df&#39;

的创建
distance <- c(1, 12, 5, 25, 7, 2, NA, 8, 19, 7, NA, 4, 16, 12, 7)
df1 <- data.frame(commute, kids, distance, stringsAsFactors=FALSE)

上面的代码可以简化

df1 %>%
    mutate(get.flyer = c("", "Yes")[(commute %in% c("walk", "bike", "subway", "ferry") &
        kids == "Yes" &
        distance < 10)+1] )

为了更好地理解,有些人更喜欢ifelse

df1 %>% 
   mutate(get.flyer = ifelse(commute %in% c("walk", "bike", "subway", "ferry") & 
                kids == "Yes" &
                distance < 10, 
                          "Yes", ""))

使用base R方法

也可以轻松完成
df1$get.flyer <- with(df1, ifelse(commute %in% c("walk", "bike", "subway", "ferry") & 
              kids == "Yes" & 
              distance < 10, 
                       "Yes", ""))

答案 1 :(得分:7)

@akrun已经指出了解决方案。我想以一种更“包裹”的方式呈现它。

您可以使用ifelse语句根据一个(或多个)条件创建列。但首先,您必须更改距离列中缺失值的“编码”。您使用""来表示缺失值,但是这会将整个列转换为string并禁止数字比较(distance < 10不可能)。指示缺失值的R方式为NA,您的distance列定义应为:

distance <- c(1, 12, 5, 25, 7, 2, NA, 8, 19, 7, NA, 4, 16, 12, 7)

ifelse语句如下所示:

df$get.flyer <- ifelse(
    ( 
        (df$commute %in% c("walk", "bike", "subway", "ferry")) &
        (df$kids == "Yes")                                     &
        (df$distance < 10)
    ),
    1,  # if condition is met, put 1
    0   # else put 0
)

可选:考虑以不同的方式对其他列进行编码:

  • 您可以使用TRUEFALSE代替“是”和“否”代表kids变量
  • 您可以使用factor进行通勤

答案 2 :(得分:0)

例如,检查first_column_name中是否包含first_column_name并将结果写入new_column

df$new_column <- apply(df, 1, function(x) grepl(x['first_column_name'], x['second_column_name'], fixed = TRUE))

详细信息:

df$new_column <- # create a new column with name new_column on df
apply(df, 1 # `1` means for each row, `apply(df` means apply the following function on df
function(x) # Function definition to apply on each row, `x` means input row for each row.
grepl(x['first_column_name'], x['second_column_name'], fixed = TRUE)) # Body of function to apply, basically run grepl to find if first_column_name is in second_column_name, fixed = TRUE means don't use regular expression just the plain text from first_column_name.