使用多个数据帧和查找表来执行r中的功能

问题描述 投票:2回答:1

我是r的新手并拥有一组复杂的数据,所以希望我的解释是正确的。我需要使用多个数据帧来执行一系列操作。这是一个例子。我有三个数据帧。一个是物种名称和相应代码的列表:

>df.sp
    Species Code
    Picea   PI
    Pinus   CA

另一个是具有不同位置(dir)的物种丰度数据的站点列表。不幸的是,物种的顺序是不同的。

>df.site
Site  dir total  t01 t02 t03 t04
2         Total   PI  CA  AB  T
2     N    9      1   5   na na
2                 AB  ZI PI CA
2     S    5     2   2  1  4
3                 DD  EE AB YT
3     N    6     1   1  5   3
3                 AB YT  EE  DD
3     S     5     4   3  1   1

然后我还有一个与物种相对应的特征数据框:

>df.trait
Species  leaft  rootl
Picea     0.01    1.2
Pinus     0.02    3.5

我想要做的一件事就是获得每个站点(df.site $ Site)和每个站点位置(df。)的每个特征(df.trait $ leaft和df.trait $ rootl)的平均值。 site $ Site N,S)。所以结果将是第一行:

Site dir leaft rootl
2    N   0.015  2.35

我希望这是有道理的。我想一想如何去做是非常复杂的。我试图从this postthis(和许多其他人)工作,但迷路了。谢谢您的帮助。非常感谢。

更新:这是使用dput的实际df.site(减少)的示例:

> dput(head(df.site))
structure(list(Site = c(2L, 2L, 2L, 2L, 2L, 2L), dir = c("rep17316", 
"N", "", "S", "", "SE"), total = c("Total", "9", "", 
"10", "", "9"), t01 = c("PI", "4", "CA", "1", "SILLAC", 
"3"), t02 = c("CXBLAN", "3", "ZIZAUR", "4", "OENPIL", "2"), 
    t03 = c("ZIZAPT", "1", "ECHPUR", "2", "ASCSYR", "2")), .Names = c("site", "dir", "total", "t01", "t02", "t03"), row.names = 2:7, class = "data.frame")
r loops merge lookup-tables
1个回答
3
投票

您将不得不首先将数据变为更清晰的形式。我假设你的dput结构在你的df.site数据帧中是一致的结构;即行是成对的,第一行指定物种代码,第二行具有计数(或一些其他收集的数据?)。

df作为上面dput()的数据帧开始,我将首先模拟其他两个数据帧的一些数据:

df.sp <- data.frame(Species = paste0("species",1:8),
                    Code = c("ECHPUR", "CXBLAN", "ZIZAPT",
                             "CAMROT", "SILLAC", "OENPIL",
                             "ASCSYR", "ZIZAUR"))
df.sp
#>    Species   Code
#> 1 species1 ECHPUR
#> 2 species2 CXBLAN
#> 3 species3 ZIZAPT
#> 4 species4 CAMROT
#> 5 species5 SILLAC
#> 6 species6 OENPIL
#> 7 species7 ASCSYR
#> 8 species8 ZIZAUR

df.trait <- data.frame(Species = paste0("species",1:8),
                       leaft = round(runif(8, max=.2), 2),
                       rootl = round(runif(8, min=1, max=4),1))

df.trait
#>    Species leaft rootl
#> 1 species1  0.12   2.5
#> 2 species2  0.04   2.6
#> 3 species3  0.12   2.1
#> 4 species4  0.05   1.1
#> 5 species5  0.15   2.5
#> 6 species6  0.15   3.3
#> 7 species7  0.05   3.9
#> 8 species8  0.13   2.1

首先,让我们通过移动包含收集数据的第二行并将这些值移动到一组新列中来清理df

library(dplyr)

df.clean <- df %>% 
  #for each row, copy the direction and total from the following row
  mutate_at(vars(matches("dir|total")), lead) %>% 
  #create new columns for observed data and fill in values from following row
  mutate_at(vars(matches("t\\d+$")), 
            .funs = funs(n = lead(.))) %>% 
  #filter to rows with species code in t01
  filter(t01 %in% df.sp$Code) %>% 
  #drop "total" column (doesn't make sense after reshape)
  select(-total)

df.clean
#>   site dir    t01    t02    t03 t01_n t02_n t03_n
#> 1    2   N ECHPUR CXBLAN ZIZAPT     4     3     1
#> 2    2   S CAMROT ZIZAUR ECHPUR     1     4     2
#> 3    2  SE SILLAC OENPIL ASCSYR     3     2     2

我们现在有两组相应的列,它们分别具有物种代码和值。要将数据帧重新整形为长格式,我们将使用data.table包中的melt()。有关如何执行此操作的其他示例,请参阅对this question的回复。

library(data.table)

df.clean <- df.clean %>% 
  setDT() %>% #convert to data.table to use data.tabel::melt
  melt(measure.vars = patterns("t\\d+$", "_n$"),
       value.name = c("Code", "Count") ) %>% 
  #drop "variable" column, which isn't needed
  select(-variable)

最后,加入您的三个数据帧:

#merge tables together
df.summaries <- df.clean %>% 
  left_join(df.sp) %>% 
  left_join(df.trait)

此时,您应该能够通过使用group_bysummarise感兴趣的任何分组来汇总数据。

© www.soinside.com 2019 - 2024. All rights reserved.