Dplyr\u如果谓词函数涉及列名和多个条件的动词?

Dplyr\u如果谓词函数涉及列名和多个条件的动词?,r,dplyr,R,Dplyr,我试图在谓词函数中使用带有列名的mutate\u if或select\u if等动词 见下例: > btest <- data.frame( + sjr_first = c('1','2','3',NA, NA, '6'), + jcr_first = c('1','2','3',NA, NA, '6'), + sjr_second = LETTERS[1:6], + jcr_second = LETTERS[1:6], + sjr_third = as.char

我试图在谓词函数中使用带有列名的mutate\u if或select\u if等动词

见下例:

> btest <- data.frame(
+   sjr_first = c('1','2','3',NA, NA, '6'),
+   jcr_first = c('1','2','3',NA, NA, '6'),
+   sjr_second = LETTERS[1:6],
+   jcr_second = LETTERS[1:6],
+   sjr_third = as.character(seq(6)),
+   jcr_fourth = seq(6) + 5,
+   stringsAsFactors = FALSE)
> 
> btest %>% select_if(.predicate = ~ str_match(names(.), 'jcr'))
Error in selected[[i]] <- eval_tidy(.p(column, ...)) : 
  replacement has length zero
我知道我可以使用btest%>%select_atvarsdplyr::matches'jcr',但我的目标实际上是将列名条件与另一个条件相结合,例如使用mutate_对我的列的子集进行操作。但是,我不确定如何使名称匹配的第一个部件工作…

您可以执行以下操作:

btest %>%
 select_if(str_detect(names(.), "jcr") & sapply(., is.numeric))

  jcr_fourth
1          6
2          7
3          8
4          9
5         10
6         11
你可以做:

btest %>%
 select_if(str_detect(names(.), "jcr") & sapply(., is.numeric))

  jcr_fourth
1          6
2          7
3          8
4          9
5         10
6         11
Tidyverse解决方案:

require(dplyr)

# Return (get):    

btest %>% 

  select_if(grepl("jcr", names(.)) & sapply(., is.numeric))

# Mutate (set):    

btest %>%

  mutate_if(grepl("jcr", names(.)) & sapply(., is.numeric), funs(paste0("whatever", .)))
# Return (get): 

btest[,grepl("jcr", names(btest)) & sapply(btest, is.numeric), drop = FALSE]

# Mutate (set): 

btest[,grepl("jcr", names(btest)) & sapply(btest, is.numeric)] <- paste0("whatever", unlist(btest[,grepl("jcr", names(btest)) & sapply(btest, is.numeric)]))
基本R解决方案:

require(dplyr)

# Return (get):    

btest %>% 

  select_if(grepl("jcr", names(.)) & sapply(., is.numeric))

# Mutate (set):    

btest %>%

  mutate_if(grepl("jcr", names(.)) & sapply(., is.numeric), funs(paste0("whatever", .)))
# Return (get): 

btest[,grepl("jcr", names(btest)) & sapply(btest, is.numeric), drop = FALSE]

# Mutate (set): 

btest[,grepl("jcr", names(btest)) & sapply(btest, is.numeric)] <- paste0("whatever", unlist(btest[,grepl("jcr", names(btest)) & sapply(btest, is.numeric)]))
Tidyverse解决方案:

require(dplyr)

# Return (get):    

btest %>% 

  select_if(grepl("jcr", names(.)) & sapply(., is.numeric))

# Mutate (set):    

btest %>%

  mutate_if(grepl("jcr", names(.)) & sapply(., is.numeric), funs(paste0("whatever", .)))
# Return (get): 

btest[,grepl("jcr", names(btest)) & sapply(btest, is.numeric), drop = FALSE]

# Mutate (set): 

btest[,grepl("jcr", names(btest)) & sapply(btest, is.numeric)] <- paste0("whatever", unlist(btest[,grepl("jcr", names(btest)) & sapply(btest, is.numeric)]))
基本R解决方案:

require(dplyr)

# Return (get):    

btest %>% 

  select_if(grepl("jcr", names(.)) & sapply(., is.numeric))

# Mutate (set):    

btest %>%

  mutate_if(grepl("jcr", names(.)) & sapply(., is.numeric), funs(paste0("whatever", .)))
# Return (get): 

btest[,grepl("jcr", names(btest)) & sapply(btest, is.numeric), drop = FALSE]

# Mutate (set): 

btest[,grepl("jcr", names(btest)) & sapply(btest, is.numeric)] <- paste0("whatever", unlist(btest[,grepl("jcr", names(btest)) & sapply(btest, is.numeric)]))
您可以将两个select_if调用分开

我们不能合并这两个调用,因为第一个调用同时处理整个数据帧,而第二个调用按列处理

您可以分隔两个select_if调用


我们不能合并这两个调用,因为第一个调用同时处理整个数据帧,而第二个调用按列处理

呃,多么愚蠢,打算使用str_-detect,我想知道为什么这个行为如此奇怪。是的,str_-match做了一些稍微不同的事情:呃,多么愚蠢,打算使用str_-detect,我想知道为什么这个行为如此奇怪。是的,str_match做了一些稍微不同的事情:旁注:发现了这个问题,其中涉及访问mutate_if函数中的列名,这似乎是一个完全不同的过程,但可能对其他搜索者有用:旁注:发现了这个问题,其中涉及访问mutate_if函数中的列名,这似乎是一个完全不同的过程,但可能对其他搜索者有用: