2

我正在尝试扩展现有数据集,目前看起来像这样:

df <- tibble(
        site = letters[1:3],
        years = rep(4, 3),
        tr = c(3, 6, 4)
)

tr 是每个站点/年份组合的重复总数。我只想添加复制,然后添加每个复制的响应变量。对于使用以下功能的单个站点/年份组合来说,这很容易:

        f <- function(site=NULL, years=NULL, t=NULL){
                df <- tibble(
                        site = rep(site, each = t, times= years),
                        tr = rep(1:t, times = years),
                        year = rep(1:years, each = t)
                        )
                df 
        }

# For one site:
f(site='a',  years=4, t=3)

# Producing this:
# # A tibble: 12 x 3
# site     tr  year
# <chr> <int> <int>
# 1 a         1     1
# 2 a         2     1
# 3 a         3     1
# 4 a         1     2
# 5 a         2     2
# 6 a         3     2
# 7 a         1     3
# 8 a         2     3
# 9 a         3     3
# 10 a         1     4
# 11 a         2     4
# 12 a         3     4

该函数如何应用于输入数据帧的每一行以生成最终数据帧?base r 中的应用函数之一或 purrr 包中的 pmap_df() 似乎很理想,但由于不熟悉这些函数的工作原理,我所有的努力都只会产生错误。

4

5 回答 5

1

如果我们想应用相同的功能,请使用pmap

library(purrr)
pmap_dfr(df, ~ f(..1, ..2, ..3))
# A tibble: 52 x 3
#   site     tr  year
# * <chr> <int> <int>
# 1 a         1     1
# 2 a         2     1
# 3 a         3     1
# 4 a         1     2
# 5 a         2     2
# 6 a         3     2
# 7 a         1     3
# 8 a         2     3
# 9 a         3     3
#10 a         1     4
# … with 42 more rows

另一种选择是condense来自开发版dplyr

library(tidyr)
df %>%
      group_by(rn = row_number()) %>% 
      condense(out = f(site, years, tr)) %>% 
      unnest(c(out))

或者 in base R,我们也可以使用do.callwithMap

do.call(rbind, do.call(Map, c(f, unname(as.data.frame(df)))))
于 2020-03-05T23:10:32.567 回答
1

在基础 R 中,你可以这样做:

do.call(rbind,do.call(Vectorize(f,SIMPLIFY = FALSE),unname(df)))
# A tibble: 52 x 3
   site     tr  year
 * <chr> <int> <int>
 1 a         1     1
 2 a         2     1
 3 a         3     1
 4 a         1     2
 5 a         2     2
 6 a         3     2
 7 a         1     3
 8 a         2     3
 9 a         3     3
10 a         1     4
# ... with 42 more rows
于 2020-03-05T23:33:17.557 回答
0
do.call(rbind, lapply(split(df, df$site), function(x){
    with(x, data.frame(site,
               years = rep(sequence(years), each = tr),
               tr = rep(sequence(tr), years)))
}))
于 2020-03-05T23:20:19.850 回答
0

Akrun 的回答对我来说效果很好,所以我对其进行了修改,以使函数更明确地应用于数据帧的每一行:


        df1 <- pmap_df(df, function(site, years, tr){
            site = rep(site, each = tr, times=years)
            year = rep(1:years, each = tr)
            tr = rep(1:tr, times=years)
          return(tibble(site, year, tr))
          })
于 2020-03-07T00:48:33.470 回答
0

我们可以使用Map应用于和的f每个值。siteyearstr

do.call(rbind, Map(f, df$site, df$years, df$tr))

# A tibble: 52 x 3
#   site     tr  year
# * <chr> <int> <int>
# 1 a         1     1
# 2 a         2     1
# 3 a         3     1
# 4 a         1     2
# 5 a         2     2
# 6 a         3     2
# 7 a         1     3
# 8 a         2     3
# 9 a         3     3
#10 a         1     4
# … with 42 more rows
于 2020-03-06T00:10:32.390 回答