按行值重命名分组tibble中的列(dplyr)

时间:2017-12-15 15:58:56

标签: r parsing dplyr aggregate grouped-table

如何在分组的dplyr tibble中重命名列,具体取决于某个行值?下图显示了我的tibble之前的状态以及它应该如何处理操作。

enter image description here

我尝试了以下代码,但没有设法编写一个列重命名函数,该函数能够从“名称”列中灵活地声明新列名。

library(dplyr)

df <- data.frame(
 "splitvar"=c(1,1,1,2,2,3,3,3,3),
 "value"=c(1,4,2,5,6,9,11,13,12),
 "name"=c("Harold","Harold","Harold","Jane","Jane","George","George","George","George"),
 stringsAsFactors=F
)

grouped_tbl <- df %>%
  group_by( splitvar ) %>%
  eval(parse(
    paste0("rename(",unique(name)," = value)")
  ))

相关:Replacement for "rename" in dplyr

2 个答案:

答案 0 :(得分:3)

像这样:

library(tidyverse)

df %>% 
  split(.$splitvar) %>% 
  map(~rename(., !!unique(.$name) := "value"))

我花了一些时间围绕着这个问题,但试着看看programming with dplyr

代码的输出是:

$`1`
  splitvar Harold   name
1        1      1 Harold
2        1      4 Harold
3        1      2 Harold

$`2`
  splitvar Jane name
4        2    5 Jane
5        2    6 Jane

$`3`
  splitvar George   name
6        3      9 George
7        3     11 George
8        3     13 George
9        3     12 George

答案 1 :(得分:1)

您可以拆分,制作新列,然后重新绑定。

以下是nest / unnest tidyr )和map purrr )的选项p>

library(tidyr)
library(purrr)

我使用rename_at替代tidyeval。

df %>%
    group_by(splitvar) %>%
    nest() %>%
    mutate(data = map(data, function(x) rename_at(x, "value", funs( unique(x$name) ) ) ) ) %>%
    unnest()

# A tibble: 9 x 5
  splitvar Harold   name  Jane George
     <dbl>  <dbl>  <chr> <dbl>  <dbl>
1        1      1 Harold    NA     NA
2        1      4 Harold    NA     NA
3        1      2 Harold    NA     NA
4        2     NA   Jane     5     NA
5        2     NA   Jane     6     NA
6        3     NA George    NA      9
7        3     NA George    NA     11
8        3     NA George    NA     13
9        3     NA George    NA     12

这可能是一次&#34;重塑&#34;我通过 tidyr 做的问题。但是,这并不会保留name列。

df %>%
    group_by(splitvar) %>%
    mutate(row = row_number() ) %>%
    spread(name, value)

# A tibble: 9 x 5
# Groups:   splitvar [3]
  splitvar   row George Harold  Jane
*    <dbl> <int>  <dbl>  <dbl> <dbl>
1        1     1     NA      1    NA
2        1     2     NA      4    NA
3        1     3     NA      2    NA
4        2     1     NA     NA     5
5        2     2     NA     NA     6
6        3     1      9     NA    NA
7        3     2     11     NA    NA
8        3     3     13     NA    NA
9        3     4     12     NA    NA