将变量名称指定为数据框头

时间:2017-11-05 10:51:33

标签: r dplyr

我有一个名为emp.data的数据框,我需要在R中将变量名称指定为数据帧标题。

数据框。

emp_id emp_name Domain salary start_date
      1     Rick    first  623.30 2012-01-01
      2 emp_name    second 515.20 2013-09-23
      3 Michelle    first  611.00 2014-11-15
      4     Ryan    Thired 729.00 2014-05-11
      5     Gary    First  843.25 2015-03-27

这里我将数据框列名称Domain分配为empFirstName变量,并将变量名称传递到dplyr下面以获取数据。

empFirstName<-"Domain"

library(dplyr)
library(tidyr)

df <- emp.data %>%
  gather(key, value, -c(emp_name, salary, empFirstName)) %>%
  group_by(emp_name, salary, empFirstName, key, value) %>%
  summarise(count = n()) %>%
  nest(key, value, count, .key = "finalValues")

但我低于错误。

FUN中的错误(X [[i]],...):找不到对象'empFirstName'

structure(list(emp_id = 1:5, emp_name = c("Rick", "emp_name", 
"Michelle", "Ryan", "Gary"), Domain = c("first", "second", "first", 
"Thired", "First"), salary = c(623.3, 515.2, 611, 729, 843.25
), start_date = structure(c(15340, 15971, 16389, 16201, 16521
), class = "Date")), .Names = c("emp_id", "emp_name", "Domain", 
"salary", "start_date"), row.names = c(NA, -5L), class = "data.frame")

2 个答案:

答案 0 :(得分:1)

我们可以sym!!一起评估&#39; empFirstName&#39;在gather中,也可以使用group_by_at

library(dplyr) #dplyr_0.7.4
library(tidyr) #tidyr_0.7.2
emp.data %>%
      gather(key, value, -c(emp_name, salary, !!rlang::sym(empFirstName)) ) %>%
      group_by_at(vars(emp_name, salary, empFirstName, key, value)) %>% 
      summarise(count = n()) %>%
      nest(key, value, count, .key = "finalValues")
# A tibble: 10 x 5
#   emp_name salary Domain        key      finalValues
#      <chr>  <dbl>  <chr>      <chr>           <list>
# 1 emp_name 515.20 second     emp_id <tibble [1 x 2]>
# 2 emp_name 515.20 second start_date <tibble [1 x 2]>
# 3     Gary 843.25  First     emp_id <tibble [1 x 2]>
# 4     Gary 843.25  First start_date <tibble [1 x 2]>
# 5 Michelle 611.00  first     emp_id <tibble [1 x 2]>
# 6 Michelle 611.00  first start_date <tibble [1 x 2]>
# 7     Rick 623.30  first     emp_id <tibble [1 x 2]>
# 8     Rick 623.30  first start_date <tibble [1 x 2]>
# 9     Ryan 729.00 Thired     emp_id <tibble [1 x 2]>
#10     Ryan 729.00 Thired start_date <tibble [1 x 2]>

答案 1 :(得分:0)

empFirstName <- quo(Domain)

emp.data %>%
  gather(key, value, -c("emp_name", "salary", !! empFirstName)) %>%
  group_by(emp_name, salary, !! empFirstName, key, value) %>%
  summarise(count = n()) %>%
  nest(key, value, count, .key = "finalValues")

# # A tibble: 10 x 5
#    emp_name salary Domain        key      finalValues
#       <chr>  <dbl>  <chr>      <chr>           <list>
#  1 emp_name 515.20 second     emp_id <tibble [1 x 2]>
#  2 emp_name 515.20 second start_date <tibble [1 x 2]>
#  3     Gary 843.25  First     emp_id <tibble [1 x 2]>
#  4     Gary 843.25  First start_date <tibble [1 x 2]>
#  5 Michelle 611.00  first     emp_id <tibble [1 x 2]>
#  6 Michelle 611.00  first start_date <tibble [1 x 2]>
#  7     Rick 623.30  first     emp_id <tibble [1 x 2]>
#  8     Rick 623.30  first start_date <tibble [1 x 2]>
#  9     Ryan 729.00 Thired     emp_id <tibble [1 x 2]>
# 10     Ryan 729.00 Thired start_date <tibble [1 x 2]>

使用上面给出的解决方案,您可以轻松创建如下函数:

myfun <- function(data, empFirstName) {
empFirstName <- enquo(empFirstName)
data %>%
  gather(key, value, -c("emp_name", "salary", !! empFirstName)) %>%
  group_by(emp_name, salary, !! empFirstName, key, value) %>%
  summarise(count = n()) %>%
  nest(key, value, count, .key = "finalValues")
}

myfun(emp.data, Domain)

# # A tibble: 10 x 5
#    emp_name salary Domain        key      finalValues
#       <chr>  <dbl>  <chr>      <chr>           <list>
#  1 emp_name 515.20 second     emp_id <tibble [1 x 2]>
#  2 emp_name 515.20 second start_date <tibble [1 x 2]>
#  3     Gary 843.25  First     emp_id <tibble [1 x 2]>
#  4     Gary 843.25  First start_date <tibble [1 x 2]>
#  5 Michelle 611.00  first     emp_id <tibble [1 x 2]>
#  6 Michelle 611.00  first start_date <tibble [1 x 2]>
#  7     Rick 623.30  first     emp_id <tibble [1 x 2]>
#  8     Rick 623.30  first start_date <tibble [1 x 2]>
#  9     Ryan 729.00 Thired     emp_id <tibble [1 x 2]>
# 10     Ryan 729.00 Thired start_date <tibble [1 x 2]>