按行r映射嵌套数据

时间:2018-11-13 03:36:28

标签: r nested apply purrr

我有类似这样的数据(再次感谢dput!):

dat <- structure(list(vars = c("var_1", "var_2"), data = list(structure(list(
  time = 1:10, value = c(1:10
  )), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
  )), structure(list(time = 1:10, value = c(11:20
  )), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
  ))), mu = c(1, 2), stdev = c(1,2)), class = c("tbl_df", "tbl", "data.frame"),
  row.names = c(NA,-2L))

我正在尝试更改一个额外的列,该列将函数映射到每一行。例如,使用dat$data[[1]]$valuedat$mu[1]dat$stdev[1]中的嵌套变量的每个元素计算dnorm,然后对第二行进行相同的操作。

我要更改的列是包含该行作为输出的每一行的小标题[10 x 1]:

dnorm(dat$data[[1]]$value, mean = dat$mu[1], sd = dat$stdev[1])
dnorm(dat$data[[2]]$value, mean = dat$mu[2], sd = dat$stdev[2])

我尝试过的东西不起作用,但可能接近吗?:

# This alternates between mean and stdev for each element of each nested variable
dat_1 <- dat %>% 
  mutate(z = map(data, ~ dnorm(.x$value, mean = dat$mu, sd = dat$stdev)))
# apply by row has structure issues
dat_2 <- dat %>% 
  apply(MARGIN = 1, function(x){
  mutate(x, z = map(data, ~ dnorm(.x$value, mean = dat$mu, sd = dat$stdev)))
    })

dat_3 <- dat %>% mutate(sigma = map(data, ~ sum(.x$value)))这样的基本映射函数可以正常工作,而无需引用df中的其他值。对我而言,以这种方式使用嵌套数据和地图还为时过早-一直在查看所有地图功能的文档以尝试解决此问题,但还算不上运气!如果这很明显,我可以尝试澄清-预先感谢!

1 个答案:

答案 0 :(得分:2)

我们可以使用平行地图:

library(purrr)
library(dplyr)

expected_out1 <- dnorm(dat$data[[1]]$value, mean = dat$mu[1], sd = dat$stdev[1])
expected_out2 <- dnorm(dat$data[[2]]$value, mean = dat$mu[2], sd = dat$stdev[2])

out <- 
  dat %>% 
  mutate(z = pmap(list(map(data, "value"), mu, stdev), dnorm))

all.equal(out$z, list(expected_out1, expected_out2))
# [1] TRUE