计算数据,按年份和区域除以R

时间:2018-11-18 00:34:55

标签: r grouping tidyverse data-management

我有一个非常大的生物数据集(无法在Excel中打开),看起来像这样

// *** sumArray() ***

function sumArray(arr){
    var total = 0;
    arr.forEach(function(element){
      total += element; 
    });
    return total;
}

我要做的是找出我拥有的三年(1980年,1985年)中每个区域(1、2或3)中每种物种(A,B或C)的数量或1990年)。

我希望得到一个看起来与此类似的数据集

    year <- c(1990, 1980, 1985, 1980, 1990, 1990, 1980, 1985, 1985,1990, 
              1980, 1985, 1980, 1990, 1990, 1980, 1985, 1985,
              1990, 1980, 1985, 1980, 1990, 1990, 1980, 1985, 1985)
    species <- c('A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'A','A', 'A', 
                 'B', 'B', 'B', 'C', 'C', 'C', 'A', 'A', 'A', 'B', 'B', 'B', 
                 'C', 'C', 'C', 'A')
    region <- c(1, 1, 1, 3, 2, 3, 3, 2, 1, 1, 3, 3, 3, 2, 2, 1, 1, 1,1, 3, 3, 
                3, 2, 2, 1, 1, 1)
    df <- data.frame(year, species, region)

    df
    year species region
 1  1990       A      1
 2  1980       A      1
 3  1985       B      1
 4  1980       B      3
 5  1990       B      2
 6  1990       C      3
 7  1980       C      3
 8  1985       C      2
 9  1985       A      1
 10 1990       A      1
 11 1980       A      3
 12 1985       B      3
 13 1980       B      3
 14 1990       B      2
 15 1990       C      2
 16 1980       C      1
 17 1985       C      1
 18 1985       A      1
 19 1990       A      1
 20 1980       A      3
 21 1985       B      3
 22 1980       B      3
 23 1990       B      2
 24 1990       C      2
 25 1980       C      1
 26 1985       C      1
 27 1985       A      1

使得每一行代表一个区域,每一列代表特定年份中每种物种的数量。我尝试使用 region A_1980 B_1980 C_1980 A_1985 B_1985 C_1985 A_1990 B_1990 C_1990 1 1 0 0 0 0 0 0 0 0 0 2 2 1 1 1 1 1 1 1 1 1 3 3 2 2 2 2 2 2 2 2 2 函数和spread dplyr函数来完成此操作,但是我无法让它做任何接近我想要的事情。

有人有什么建议吗?

2 个答案:

答案 0 :(得分:11)

像这样吗?

library(dplyr)

df2 <- df %>% 
  mutate(sp_year = paste(species, year, sep = "_")) %>%
  group_by(region) %>% 
  count(sp_year) %>% 
  spread(sp_year,n)

df2

哪个给这个:

# A tibble: 3 x 10
# Groups:   region [3]
  region A_1980 A_1985 A_1990 B_1980 B_1985 B_1990 C_1980 C_1985 C_1990
   <dbl>  <int>  <int>  <int>  <int>  <int>  <int>  <int>  <int>  <int>
1      1      1      3      3     NA      1     NA      2      2     NA
2      2     NA     NA     NA     NA     NA      3     NA      1      2
3      3      2     NA     NA      3      2     NA      1     NA      1

答案 1 :(得分:5)

类似于wl1234的答案,但更为简洁。我们可以使用unite来合并列。我们也可以使用count而不使用变量group_by。最后,我们可以在fill = 0函数中设置spread以将NA替换为0。

library(tidyverse)

df2 <- df %>%
  unite(sp_year, species, year, sep = "_") %>%
  count(sp_year, region) %>%
  spread(sp_year, n, fill = 0)
df2
# # A tibble: 3 x 10
#   region A_1980 A_1985 A_1990 B_1980 B_1985 B_1990 C_1980 C_1985 C_1990
#    <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
# 1      1      1      3      3      0      1      0      2      2      0
# 2      2      0      0      0      0      0      3      0      1      2
# 3      3      2      0      0      3      2      0      1      0      1