r语言 - Ifelse适用于DataFrame中的多个列



我有一个像这样的数据集:

<表类> ID 冬天春天夏季秋季tbody><<tr>1高NA高低2低高NA低3低NANA低4低高NA低

我们可以用if_any

library(dplyr)
df1 <- df1 %>%
mutate(calculated_column = +(if_any(-ID, ~ . %in% 'high')))

与产出

df1
ID Winter Spring Summer Fall calculated_column
1  1   high   <NA>   high  low                 1
2  2    low   high   <NA>  low                 1
3  3    low   <NA>   <NA>  low                 0
4  4    low   high   <NA>  low                 1

或者如果我们想使用base R,则在逻辑矩阵

上创建rowSums的逻辑条件
df1$calculated_column <-  +(rowSums(df1[-1] == "high", na.rm = TRUE) > 0)

数据
df1 <- structure(list(ID = 1:4, Winter = c("high", "low", "low", "low"
), Spring = c(NA, "high", NA, "high"), Summer = c("high", NA, 
NA, NA), Fall = c("low", "low", "low", "low")), 
class = "data.frame", row.names = c(NA, 
-4L))

你也可以这样做:

df1$calculated_column = +grepl('high', do.call(paste, df1))
df1
ID Winter Spring Summer Fall calculated_column
1  1   high   <NA>   high  low                 1
2  2    low   high   <NA>  low                 1
3  3    low   <NA>   <NA>  low                 0
4  4    low   high   <NA>  low                 1

这是base R的解决方案:

calculated_column = (apply(df1,1,function(x) sum(grepl("high",x)))>0)*1
cbind(df1, calculated_column) 
ID Winter Spring Summer Fall calculated_column
1  1   high   <NA>   high  low                 1
2  2    low   high   <NA>  low                 1
3  3    low   <NA>   <NA>  low                 0
4  4    low   high   <NA>  low                 1

相关内容

  • 没有找到相关文章

最新更新