2017-10-11 48 views
0

我試圖找到分組變量中所有變量的關聯。具體而言,我試圖用purrr來做到這一點,以取代我一直在使用的循環。但是我遇到了一些困難,部分原因是我想在使用感興趣的矢量時使用兩個函數。例如:嘗試用purrr :: map_dbl替換一個循環,並使用corrr :: correlate

## load packages 
library(corrr) 
library(dplyr) 
library(purrr) 

沒有任何的團體能正常工作(這就是想什麼我做底座):

iris %>% 
    select(-Species) %>% 
    correlate() %>% 
    stretch() 

但是當我嘗試組我得到這個陷入困境:

iris %>% 
    group_by(Species) %>% 
    correlate() %>% 
    stretch() 

Error in stats::cor(x = x, y = y, use = use, method = method) : 'x' must be numeric

所以我的想法是使用purrr ...好像在哪裏使用它的權利的確切地點?

iris %>% 
    split(.$Species) %>% 
    map_dbl(~correlate) ## then how do i incorporate `stretch()` 

Error: Can't coerce element 1 from a closure to a double

這顯然是錯誤的,但我不知道到底我應該申請map_*這裏...

這就是我試圖取代它並給出正確的輸出循環,但我寧願不使用它 - 它比purrr方法靈活:

Species <- unique(iris$Species) 
df <- c() 
for(i in seq_along(Species)){ 
    u <- iris %>% 
    filter(Species == Species[i]) %>% 
    select(-Species) %>% 
    correlate() %>% 
    stretch() %>% 
    mutate(Species = Species[i]) 

    df <- rbind(df, u) 
} 

df 

# A tibble: 48 x 4 
       x   y   r Species 
      <chr>  <chr>  <dbl> <fctr> 
1 Sepal.Length Sepal.Length  NA setosa 
2 Sepal.Length Sepal.Width 0.7425467 setosa 
3 Sepal.Length Petal.Length 0.2671758 setosa 
4 Sepal.Length Petal.Width 0.2780984 setosa 
5 Sepal.Width Sepal.Length 0.7425467 setosa 
6 Sepal.Width Sepal.Width  NA setosa 
7 Sepal.Width Petal.Length 0.1777000 setosa 
8 Sepal.Width Petal.Width 0.2327520 setosa 
9 Petal.Length Sepal.Length 0.2671758 setosa 
10 Petal.Length Sepal.Width 0.1777000 setosa 

所以總而言之,有人可以概括瞭如何使用purrr當我需要使用兩個功能。換句話說,我如何替換上面的循環?

回答

1

你需要更靈活的總結語法與group_by %>% do,凡在do,你可以用.訪問每個組和應用correlatestretch就像一個正常的數據幀:

library(corrr) 
library(dplyr) 

iris %>% group_by(Species) %>% do(
    select(., -Species) %>% correlate() %>% stretch() 
) 

# A tibble: 48 x 4 
# Groups: Species [3] 
# Species   x   y   r 
# <fctr>  <chr>  <chr>  <dbl> 
# 1 setosa Sepal.Length Sepal.Length  NA 
# 2 setosa Sepal.Length Sepal.Width 0.7425467 
# 3 setosa Sepal.Length Petal.Length 0.2671758 
# 4 setosa Sepal.Length Petal.Width 0.2780984 
# 5 setosa Sepal.Width Sepal.Length 0.7425467 
# 6 setosa Sepal.Width Sepal.Width  NA 
# 7 setosa Sepal.Width Petal.Length 0.1777000 
# 8 setosa Sepal.Width Petal.Width 0.2327520 
# 9 setosa Petal.Length Sepal.Length 0.2671758 
#10 setosa Petal.Length Sepal.Width 0.1777000 
# ... with 38 more rows 

隨着purrr,你可以先在每個組下面嵌套數據,然後在其上面寫上map

library(purrr) 
library(tidyr) 
library(dplyr) 

iris %>% 
    group_by(Species) %>% nest() %>% 
    mutate(data = map(data, compose(stretch, correlate))) %>% 
    unnest() 

# A tibble: 48 x 4 
# Species   x   y   r 
# <fctr>  <chr>  <chr>  <dbl> 
# 1 setosa Sepal.Length Sepal.Length  NA 
# 2 setosa Sepal.Length Sepal.Width 0.7425467 
# 3 setosa Sepal.Length Petal.Length 0.2671758 
# 4 setosa Sepal.Length Petal.Width 0.2780984 
# 5 setosa Sepal.Width Sepal.Length 0.7425467 
# 6 setosa Sepal.Width Sepal.Width  NA 
# 7 setosa Sepal.Width Petal.Length 0.1777000 
# 8 setosa Sepal.Width Petal.Width 0.2327520 
# 9 setosa Petal.Length Sepal.Length 0.2671758 
#10 setosa Petal.Length Sepal.Width 0.1777000 
# ... with 38 more rows 
+0

好的!我想等一會兒接受這個,因爲我想知道是否有一個以咕嚕聲爲基礎的答案。或者你是否說在purrr中無法做到這一點? – boshek

+0

'purrr'中有很多方法可以做到這一點。我以爲'group_by%>%do'已經很簡單了。爲'purrr'提供了一個替代方案,請參閱更新。 – Psidom

+0

雙倍幫助!我的理解是,做事的首選方式是通過map_ *而不是'do',這就是爲什麼我要求這種做法。 – boshek