2017-05-24 9 views
1

purrr 라이브러리 함수 pmap()을 여러 조건의 if 문을 포함하는 함수와 함께 사용할 때마다 if 문이 제대로 작동하지 않는 것 같습니다. 무슨 뜻인지 보여주기 위해, 여기 gapminder 데이터 세트를 사용하여 재현 가능한 예제가 있습니다.purrr :: pmap 및 여러 조건의 함수

library(tidyverse) 
library(gapminder) 
library(broom) 

# Nest the tibble into separate dataframes for each country-continent combination 

by_country <- gapminder %>% 
    group_by(country, continent) %>% 
    nest() 

는 지금은 각 그룹화 dataframe에 대한 선형 회귀 모델을 구축하고자합니다. 국가 및 대륙에 따라 내 모델에서 다른 x- 변수를 사용하고자한다는 점이 중요합니다.

# My function 
country_model <- function(df, cont, count) { 

    if(cont == "Asia" & count == "Afghanistan") { # 2 conditions 

    lm(lifeExp ~ year, data = df) 

    } else { 

    lm(lifeExp ~ pop, data = df) 

    } 
} 

지금 내가 그 기능을 모든 그룹화 dataframes에 적용거야 : 나는 if 문에 문제가 의심되는 경우 여기 제 기능입니다. 아프가니스탄 데이터 세트의 모델이 pop이 아닌 year에 대한 계수를 가질 것이라는 모델 요약 출력이 표시됩니다. 아프가니스탄 계수 pop하지 year 것을 나타내는

by_country2 <- by_country %>% 
    mutate(model = pmap(list(data, continent, country), country_model), 
     modelsum = map(model, tidy)) %>% 
    unnest(modelsum, .drop = TRUE) 

by_country2 

내 출력.

country_model <- function(df, cont) { 

    if(cont == "Asia") { # Only 1 condition 

    lm(lifeExp ~ year, data = df) 

    } else { 

    lm(lifeExp ~ pop, data = df) 

    } 
} 

by_country2 <- by_country %>% 
    mutate(model = map2(data, continent, country_model), 
     modelsum = map(model, tidy)) %>% 
    unnest(modelsum, .drop = TRUE) 

by_country2 

# A tibble: 284 × 7 
     country continent  term  estimate std.error statistic  p.value 
     <fctr> <fctr>  <chr>   <dbl>  <dbl>  <dbl>  <dbl> 
1 Afghanistan  Asia (Intercept) -5.075343e+02 4.048416e+01 -12.536613 1.934055e-07 
2 Afghanistan  Asia  year 2.753287e-01 2.045093e-02 13.462890 9.835213e-08 
3  Albania Europe (Intercept) 4.963274e+01 1.935933e+00 25.637630 1.871817e-10 
4  Albania Europe   pop 7.286188e-06 7.171585e-07 10.159802 1.374311e-06 
5  Algeria Africa (Intercept) 3.565187e+01 1.632853e+00 21.834099 9.087006e-10 
6  Algeria Africa   pop 1.176242e-06 7.588190e-08 15.500960 2.548769e-08 
7  Angola Africa (Intercept) 2.855043e+01 1.922225e+00 14.852803 3.843692e-08 
8  Angola Africa   pop 1.276860e-06 2.482137e-07 5.144195 4.351004e-04 
9 Argentina Americas (Intercept) 5.323586e+01 3.784907e-01 140.653008 8.102227e-18 
10 Argentina Americas   pop 5.532629e-07 1.282987e-08 43.123018 1.079775e-12 
# ... with 274 more rows 

내 문제가 함께 있는지 확실하지 않습니다 :

A tibble: 284 × 7 
     country continent  term  estimate std.error statistic  p.value 
     <fctr> <fctr>  <chr>  <dbl>  <dbl>  <dbl>  <dbl> 
1 Afghanistan  Asia (Intercept) 2.834615e+01 2.314395e+00 12.247758 2.410050e-07 
2 Afghanistan  Asia   pop 5.771517e-07 1.343425e-07 4.296121 1.570999e-03 
3  Albania Europe (Intercept) 4.963274e+01 1.935933e+00 25.637630 1.871817e-10 
4  Albania Europe   pop 7.286188e-06 7.171585e-07 10.159802 1.374311e-06 
5  Algeria Africa (Intercept) 3.565187e+01 1.632853e+00 21.834099 9.087006e-10 
6  Algeria Africa   pop 1.176242e-06 7.588190e-08 15.500960 2.548769e-08 
7  Angola Africa (Intercept) 2.855043e+01 1.922225e+00 14.852803 3.843692e-08 
8  Angola Africa   pop 1.276860e-06 2.482137e-07 5.144195 4.351004e-04 
9 Argentina Americas (Intercept) 5.323586e+01 3.784907e-01 140.653008 8.102227e-18 
10 Argentina Americas   pop 5.532629e-07 1.282987e-08 43.123018 1.079775e-12 
# ... with 274 more rows 

무엇을 나에게 이상한 것은 내 기능 if 문에서 단 1 조건을 사용할 때, 완벽하게 작동하는 것 같다이다 pmap() 또는 내 if 문.

+0

이 경우 깔끔한 목적은 무엇인가 :

문자 강요는 문제를 해결? – Michael

+0

@ 마이클, 당신이 뭘 말하고 있는지 잘 모르겠습니까? 'by_country' 객체는 무엇입니까? 'by_country2' 객체는 무엇입니까? – RNB

+0

@RNB, 마이클의 혼란은 'broom'에 대한 라이브러리 호출이 누락되어 코드가'tidy '라는 누락 된 객체에 대한 오류를 표시하게합니다. – Axeman

답변

1

이것은 this GitHub issue과 관련됩니다.

country은 숫자로 표시되며, 함수에 print 문을 넣음으로써 확인할 수 있습니다.

test_fun <- function(df, cont, xx) { 
    print(paste(cont, xx)) 
} 

temp <-by_country %>% 
    mutate(model = pmap(list(data, continent, country), test_fun)) 

인쇄합니다 :

[1] "3 1" 
[1] "4 2" 
[1] "1 3" 
[1] "1 4" 
[1] "2 5" 
[1] "5 6" 
[1] "4 7" 
[1] "3 8" 
[1] "3 9" 
etc 

map2에서 발생하지 않기 때문에 두 번째 시도는 작업을 수행합니다.

by_country %>% 
    mutate(model = pmap(list(data, as.character(continent), as.character(country)), country_model), 
     modelsum = map(model, broom::tidy)) %>% 
    unnest(modelsum, .drop = TRUE) 
# A tibble: 284 x 7 
     country continent  term  estimate std.error statistic  p.value 
     <fctr> <fctr>  <chr>   <dbl>  <dbl>  <dbl>  <dbl> 
1 Afghanistan  Asia (Intercept) -5.075343e+02 4.048416e+01 -12.536613 1.934055e-07 
2 Afghanistan  Asia  year 2.753287e-01 2.045093e-02 13.462890 9.835213e-08 
3  Albania Europe (Intercept) 4.963274e+01 1.935933e+00 25.637630 1.871817e-10 
4  Albania Europe   pop 7.286188e-06 7.171585e-07 10.159802 1.374311e-06 
5  Algeria Africa (Intercept) 3.565187e+01 1.632853e+00 21.834099 9.087006e-10 
6  Algeria Africa   pop 1.176242e-06 7.588190e-08 15.500960 2.548769e-08 
7  Angola Africa (Intercept) 2.855043e+01 1.922225e+00 14.852803 3.843692e-08 
8  Angola Africa   pop 1.276860e-06 2.482137e-07 5.144195 4.351004e-04 
9 Argentina Americas (Intercept) 5.323586e+01 3.784907e-01 140.653008 8.102227e-18 
10 Argentina Americas   pop 5.532629e-07 1.282987e-08 43.123018 1.079775e-12 
# ... with 274 more rows 
+0

감사합니다, 애스크 맨! 매력처럼 일했습니다. GitHub에서 purrr로 문제를 기록 할 가치가 있다고 생각합니까? 아니면 정말로 문제가되지 않습니까? – RNB

+0

아, 문제에 관해 위의 의견을 보았습니다. 감사 :) – RNB