2017-11-05 6 views
0

私はemp.dataという名前のデータフレームを1つ持っています。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 

ここで私は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 'はまた、我々はgatherで 'empFirstName' を評価する!!symを使用することができます

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") 
+0

empFirstNameを 'group_by'から削除し、empFirstName = Domainで置き換えます。 – Santosh

答えて

1

を発見していませんあなたは簡単にこのような関数を作成することができる上に与えられたソリューションを使用して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]> 
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]> 
関連する問題