2017-09-15 3 views
1

합계를 구성해야하는 열 목록에서 데이터 열 하나 이상을 합산하려고합니다. 예를 들어문자 벡터를 기준으로 데이터 프레임에서 동적 열 수 만들기

:

set.seed(3550) 
# Creates data frame 
month <- seq.Date(from = as.Date("2012-09-01"), by = "month", length.out = 50) 
a <- rpois(50, 5000) 
b <- rpois(50, 3000) 
c <- rpois(50, 500) 
d <- rpois(50, 1000) 

df <- data.frame(month, a, b, c, d) 
# Creates list of vectors 
mylist <- list(this = "this", that = "that", other = "other") 
mylist$this <- c("a") 
mylist$that <- c("a", "b") 
mylist$other <- c("a", "c", "d") 

나는 다음과 같은 코드를 내가 원하는 결과를 얻을 수 있습니다 :

# A tibble: 50 x 4 
     month this that other 
     <date> <int> <int> <int> 
1 2012-09-01 4958 7858 6480 
2 2012-10-01 4969 7915 6497 
3 2012-11-01 5012 7978 6483 
4 2012-12-01 4982 7881 6460 
5 2013-01-01 4838 7880 6346 
6 2013-02-01 5090 8089 6589 
7 2013-03-01 5013 8044 6582 
8 2013-04-01 4947 7942 6388 
9 2013-05-01 5065 8124 6506 
10 2013-06-01 5020 8086 6521 
# ... with 40 more rows 

내가하려고 문제로 실행 : 출력 존재와

my_df <- df %>% 
    group_by(month) %>% 
    summarize(this = sum(!!!rlang::syms(mylist$this), na.rm = TRUE), 
      that = sum(!!!rlang::syms(mylist$that), na.rm = TRUE), 
      other = sum(!!!rlang::syms(mylist$other), na.rm = TRUE)) 

을 요약 된 컬럼의 수를 동적으로 작성하는 f}을 이해할 수 있습니다. 나는 요약 호출 내에서 루핑이 작동한다고 생각했지만 그렇지 않았다.

combine_iterations <- function(x, iter_list){ 
    a <- rlang::syms(names(iter_list)) 
    b <- x %>% 
    group_by(month) %>% 
    summarize(for (i in 1:length(a)){ 
     a[[i]] = sum(!!!rlang::syms(iter_list[i]), na.rm = TRUE) 
    }) 
} 

출력 :

Error in lapply(.x, .f, ...) : object 'i' not found 
Called from: lapply(.x, .f, ...) 
+1

것에 세상은'!!!'인가? –

+0

@KyleWeise 표준 평가 기능이 더 이상 사용되지 않을 때 dplyr에 추가 된 견적/인용 취소 정비사의 일부입니다. 특히 그것은 따옴표로 묶지 않은 스플 라이스입니다. –

답변

2

당신은 복잡한에 그것을 조금하고 있습니다; 당신이 요약을 사용자 정의하려면, 당신은 group_by %>% do을 사용하고 rlang 인용/맺다 문제를 방지 할 수 있습니다


combine_iterations <- function(x, iter_list){ 
    x %>% 
     group_by(month) %>% 
     do(
      as.data.frame(lapply(iter_list, function(cols) sum(.[cols]))) 
    ) 
} 

combine_iterations(df, mylist) 
# A tibble: 50 x 4 
# Groups: month [50] 
#  month this that other 
#  <date> <int> <int> <int> 
# 1 2012-09-01 5144 8186 6683 
# 2 2012-10-01 5134 8090 6640 
# 3 2012-11-01 4949 7917 6453 
# 4 2012-12-01 5040 8203 6539 
# 5 2013-01-01 4971 7938 6474 
# 6 2013-02-01 5050 7924 6541 
# 7 2013-03-01 5018 8022 6579 
# 8 2013-04-01 4945 7987 6476 
# 9 2013-05-01 5134 8114 6590 
#10 2013-06-01 4984 8011 6476 
# ... with 40 more rows 

identical(
    df %>% 
     group_by(month) %>% 
     summarise(this = sum(a), that = sum(a, b), other = sum(a, c, d)), 

    ungroup(combine_iterations(df, mylist)) 
) 
# [1] TRUE 
또는 do에서 purrr::map_df 또 다른 옵션 생성 데이터 프레임 :

combine_iterations <- function(x, iter_list){ 
    x %>% 
     group_by(month) %>% 
     do({ 
      g = . 
      map_df(iter_list, ~ sum(g[.x])) 
     }) 
} 
+0

나는 당신의 해결책을 purrr : map_df()로 보았습니다. 왜 이것이 바람직한가? 그것이베이스 R에서 완료 되었기 때문에? –

+0

저는 실제로 map_df를 선호하지만, 혼란을 가져올 수 있다고 생각합니다. 두 번째 옵션으로 추가합니다. – Psidom

관련 문제