How should a function be applied by row on a dataframe to generate a new or expanded dataframe in r

Viewed 45

I am trying to expand an existing dataset, which currently looks like this:

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

tr is the total number of replicates for each site/year combination. I simply want to add in the replicates and later the response variable for each replicate. This was easy for a single site/year combination using the following function:

        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

How can the function be applied to each row of the input dataframe to produce the final dataframe? One of the apply functions in base r or the pmap_df() in the purrr package would seem ideal, but being unfamiliar with how these functions work, all my efforts have only produced errors.

5 Answers

If we want to apply the same function, use 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

another option is condense from the devel version of dplyr

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

Or in base R, we can also use do.call with Map

do.call(rbind, do.call(Map, c(f, unname(as.data.frame(df)))))

well in base R, you could do:

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
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)))
}))

We can use Map to apply f to every value of site, years and tr.

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

Akrun's answer worked well for me, so I modified it to make the function to be applied to each row of the dataframe a little more explicit:


        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))
          })
Related