R - dplyr - mutate_if 多个条件
R - dplyr - mutate_if multiple conditions
我想根据多个条件改变列。例如,对于最大值为 5 且列名包含 "xy" 的每一列,应用一个函数。
df <- data.frame(
xx1 = c(0, 1, 2),
xy1 = c(0, 5, 10),
xx2 = c(0, 1, 2),
xy2 = c(0, 5, 10)
)
> df
xx1 xy1 xx2 xy2
1 0 0 0 0
2 1 5 1 5
3 2 10 2 10
df2 <- df %>% mutate_if(~max(.)==10, as.character)
> str(df2)
'data.frame': 3 obs. of 4 variables:
$ xx1: num 0 1 2
$ xy1: chr "0" "5" "10"
$ xx2: num 0 1 2
$ xy2: chr "0" "5" "10"
#function worked
df3 <- df %>% mutate_if(str_detect(colnames(.), "xy"), as.character)
> str(df3)
'data.frame': 3 obs. of 4 variables:
$ xx1: num 0 1 2
$ xy1: chr "0" "5" "10"
$ xx2: num 0 1 2
$ xy2: chr "0" "5" "10"
#Worked again
现在当我尝试组合它们时
df4 <- df %>% mutate_if((~max(.)==10) & (str_detect(colnames(.), "xy")), as.character)
Error in (~max(.) == 10) & (str_detect(colnames(.), "xy")) :
operations are possible only for numeric, logical or complex types
我错过了什么?
必须使用 names
而不是 colnames
df4 <- df %>% mutate_if((max(.)==10 & str_detect(names(.), "xy")), as.character)
更简洁的方法是使用来自 dplyr
的 across
df4 <- df %>% mutate(across(c(where(function(x)max(x)==10),contains('xy')),as.character))
我想根据多个条件改变列。例如,对于最大值为 5 且列名包含 "xy" 的每一列,应用一个函数。
df <- data.frame(
xx1 = c(0, 1, 2),
xy1 = c(0, 5, 10),
xx2 = c(0, 1, 2),
xy2 = c(0, 5, 10)
)
> df
xx1 xy1 xx2 xy2
1 0 0 0 0
2 1 5 1 5
3 2 10 2 10
df2 <- df %>% mutate_if(~max(.)==10, as.character)
> str(df2)
'data.frame': 3 obs. of 4 variables:
$ xx1: num 0 1 2
$ xy1: chr "0" "5" "10"
$ xx2: num 0 1 2
$ xy2: chr "0" "5" "10"
#function worked
df3 <- df %>% mutate_if(str_detect(colnames(.), "xy"), as.character)
> str(df3)
'data.frame': 3 obs. of 4 variables:
$ xx1: num 0 1 2
$ xy1: chr "0" "5" "10"
$ xx2: num 0 1 2
$ xy2: chr "0" "5" "10"
#Worked again
现在当我尝试组合它们时
df4 <- df %>% mutate_if((~max(.)==10) & (str_detect(colnames(.), "xy")), as.character)
Error in (~max(.) == 10) & (str_detect(colnames(.), "xy")) : operations are possible only for numeric, logical or complex types
我错过了什么?
必须使用 names
而不是 colnames
df4 <- df %>% mutate_if((max(.)==10 & str_detect(names(.), "xy")), as.character)
更简洁的方法是使用来自 dplyr
的 acrossdf4 <- df %>% mutate(across(c(where(function(x)max(x)==10),contains('xy')),as.character))