Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何根据条件提取列名?_R_Dplyr_Purrr - Fatal编程技术网

R 如何根据条件提取列名?

R 如何根据条件提取列名?,r,dplyr,purrr,R,Dplyr,Purrr,考虑这个简单的例子 mytest <- data_frame(group = c('a', 'a', 'a', 'b', 'b', 'b'), x = c(NA,NA,NA,5,6,7), other_var = c(NA, NA, NA, 1,2,3), y = c(3,5,6,NA,NA,NA), another_var

考虑这个简单的例子

mytest <- data_frame(group = c('a', 'a', 'a', 'b', 'b', 'b'),
                     x = c(NA,NA,NA,5,6,7),
                     other_var = c(NA, NA, NA, 1,2,3),
                     y = c(3,5,6,NA,NA,NA),
                     another_var = c(1,2,3, NA,NA,NA),
                     label_x = c('hello','hello','hello','world','world','world'),
                     label_y =c('bada','bada','bada','boom','boom','boom'),
                     label_other_var = c('ak','ak','ak','run','run','run'),
                     label_another_var = c('noo','noo','noo','bie','bie','bie'))

# A tibble: 6 x 9
  group     x other_var     y another_var label_x label_y label_other_var label_another_var
  <chr> <dbl>     <dbl> <dbl>       <dbl> <chr>   <chr>   <chr>           <chr>            
1 a        NA        NA     3           1 hello   bada    ak              noo              
2 a        NA        NA     5           2 hello   bada    ak              noo              
3 a        NA        NA     6           3 hello   bada    ak              noo              
4 b         5         1    NA          NA world   boom    run             bie              
5 b         6         2    NA          NA world   boom    run             bie              
6 b         7         3    NA          NA world   boom    run             bie 
事实上,以a组为例。只有一个非缺失变量是y和另一个_变量。但是,y的名称是bada,如标签_y变量所示,另一个_变量的名称是noo。b的理由也一样

我不知道在跑步后如何通过地图调用来实现这一点

mytest %>% group_by(group) %>% nest()

# A tibble: 2 x 2
  group data            
  <chr> <list>          
1 a     <tibble [3 x 8]>
2 b     <tibble [3 x 8]>
有什么想法吗? 谢谢

编辑:最初的,较小的,tibble提议如下

 mytest <- data_frame(group = c('a', 'a', 'a', 'b', 'b', 'b'),
+                      x = c(NA,NA,NA,5,6,7),
+                      y = c(3,5,6,NA,NA,NA),
+                      label_x = c('hello','hello','hello','world','world','world'),
+                      label_y =c('bada','bada','bada','boom','boom','boom'))
按嵌套分组后,使用map循环“data”,通过提取第一个非NA元素总结“label”列,将其收集到一个列,同时删除NA NA.rm=TRUE,选择“var”列,然后在仅保留感兴趣的列后执行unnest

mytest %>%
  group_by(group) %>% 
  nest %>% 
  mutate(var = map(data, ~ 
                     .x %>%
                      summarise(label_x = label_x[!is.na(x)][1], 
                                label_y = label_y[!is.na(y)][1]) %>% 
                      gather(key, var, na.rm = TRUE) %>% 
                      select(var))) %>%
  select(-data) %>% 
  unnest
# A tibble: 2 x 2#
#  group var 
#  <chr> <chr>
#1 a     bada 
#2 b     world

这将输出您想要的结果:

mytest <- data_frame(group = c('a', 'a', 'a', 'b', 'b', 'b'),
                     x = c(NA,NA,NA,5,6,7),
                     y = c(3,5,6,NA,NA,NA),
                     label_x = c('hello','hello','hello','world','world','world'),
                     label_y =c('bada','bada','bada','boom','boom','boom'))

extract_good_colnames <- function(df, subgroup){
  subset <- filter(df, group == subgroup)
  if(sum(is.na(subset$x)) > 0){
    colname = 'label_y'
  }else if(sum(is.na(subset$y)) > 0){
    colname = 'label_x'
  }
  return(tibble(group = subgroup, var = as.character(subset[1, colname])))
}

groups <- unique(mytest$group)
map_df(groups, function(x) extract_good_colnames(mytest, x))

嗨,阿克伦,这很好,但我不能真正概括。也就是说,在我的dataframe中,有许多不同的变量具有不同的名称。但是,标签变量的形式始终为label_X,其中X是给定的变量。我正在考虑在这里使用summary_,但没有看到一条清晰的道路。。。有什么想法吗?谢谢@ℕʘʘḆḽḘ 我正要问你这件事。如果您有一个相应的标签列,实际的列名是后缀part,那么我们可以通过循环列的名称来完成。是的,让我创建一个稍微大一点的示例,这样循环就更有意义了。再次感谢!完成!再次感谢,是的,这在现实生活中非常有用:@ℕʘʘḆḽḘ 更新后,您还可以保留原始数据集并将其作为更新,这样我就不必更改原始答案。我会更新答案好的,我也可以添加旧答案
nm1 <- unique(sub("label_", "", setdiff(names(mytest), "group")))
nm2 <- paste0("label_", nm1)
mytest %>% 
   group_by(group) %>% 
   nest %>%
   mutate(var = map(data, ~ 
                    map2_chr(.x %>% 
                               select(nm1),
                             .x %>%
                              select(nm2), ~ 
                                .y[!is.na(.x)][1]) %>% 
                                   na.omit %>%
                                   tibble(var = .))) %>% 
    select(-data) %>%
    unnest
# A tibble: 4 x 2
#  group var  
#  <chr> <chr>
#1 a     bada 
#2 a     noo  
#3 b     world
#4 b     run  
mytest <- data_frame(group = c('a', 'a', 'a', 'b', 'b', 'b'),
                     x = c(NA,NA,NA,5,6,7),
                     y = c(3,5,6,NA,NA,NA),
                     label_x = c('hello','hello','hello','world','world','world'),
                     label_y =c('bada','bada','bada','boom','boom','boom'))

extract_good_colnames <- function(df, subgroup){
  subset <- filter(df, group == subgroup)
  if(sum(is.na(subset$x)) > 0){
    colname = 'label_y'
  }else if(sum(is.na(subset$y)) > 0){
    colname = 'label_x'
  }
  return(tibble(group = subgroup, var = as.character(subset[1, colname])))
}

groups <- unique(mytest$group)
map_df(groups, function(x) extract_good_colnames(mytest, x))