映射_df()以生成类似于'rbind`ed`lapply()的输出`
下面我的映射_df()以生成类似于'rbind`ed`lapply()的输出`,r,dataframe,loops,tidyverse,purrr,R,Dataframe,Loops,Tidyverse,Purrr,下面我的map\u df()调用是否有方法生成与下面所需的输出类似的输出 library(nlme) library(tidyverse) dat <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/var.csv') dat$fmonth <- factor(dat$month) m5 <- lme(y ~ x*fmonth, random = ~1|id, data = dat, weig
map\u df()
调用是否有方法生成与下面所需的输出类似的输出
library(nlme)
library(tidyverse)
dat <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/var.csv')
dat$fmonth <- factor(dat$month)
m5 <- lme(y ~ x*fmonth, random = ~1|id, data = dat, weights = varPower(form=~x|fmonth),
control = lmeControl(msMaxIter = 1e2))
hetro_var <- function(fit) coef(fit$modelStruct$varStruct, uncons = FALSE, allCoef = TRUE)
x_fmonth1 <- map_df(hetro_var(m5), ~sigma(m5)^2*abs(dat$x)^(2*hetro_var(m5)[.])) # Can this produce desired_output?
x_fmonth2 <- lapply(names(hetro_var(m5)), function(i)sigma(m5)^2*abs(dat$x)^(2*hetro_var(m5)[i]))
names(x_fmonth2) <- names(hetro_var(m5))
desired_output <- bind_rows(x_fmonth2) # can `map_df()` above produce this output?
库(nlme)
图书馆(tidyverse)
dat您可以使用map\u df()
。只需传入hetro_var(m5)
的名称,并将该列表本身命名即可
mapdf_输出%
集合名称(名称(hetro变量(m5)))%>%
map_df(函数(i)sigma(m5)^2*abs(dat$x)^(2*hetro_var(m5)[i]))
assertthat::are_equal(mapdf_输出,所需_输出)#TRUE
在某些情况下,我们必须用自己的值命名map
输入,这似乎有点可笑-我之前问过这个问题,似乎无法绕过set\u names()
复制。您可以使用map\u dfc
并将名称作为输入传递给lappy
调用
purrr::map_dfc(names(hetro_var(m5)),
~tibble(!!.x := sigma(m5)^2*abs(dat$x)^(2*hetro_var(m5)[.x])))
# A tibble: 768 x 12
# `5` `6` `9` `10` `12` `3` `4` `11` `8` `2` `1` `7`
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 0.566 0.612 0.864 0.861 0.498 0.448 0.401 0.582 0.160 0.474 0.355 0.276
# 2 0.691 0.748 1.06 1.06 0.608 0.546 0.487 0.710 0.192 0.578 0.431 0.334
# 3 0.253 0.273 0.378 0.377 0.224 0.203 0.182 0.260 0.0757 0.214 0.162 0.127
# 4 0.484 0.523 0.735 0.733 0.426 0.384 0.343 0.497 0.138 0.406 0.304 0.237
# 5 0.376 0.406 0.568 0.567 0.332 0.300 0.269 0.386 0.109 0.317 0.239 0.186
# 6 0.335 0.361 0.504 0.503 0.296 0.267 0.239 0.344 0.0981 0.282 0.213 0.167
# 7 0.524 0.566 0.797 0.795 0.461 0.415 0.371 0.538 0.149 0.439 0.329 0.256
# 8 0.229 0.247 0.342 0.341 0.203 0.184 0.165 0.235 0.0691 0.194 0.147 0.116
# 9 0.261 0.282 0.391 0.390 0.231 0.209 0.188 0.268 0.0780 0.221 0.167 0.131
#10 0.174 0.187 0.258 0.257 0.154 0.140 0.126 0.178 0.0535 0.148 0.113 0.0889
# … with 758 more rows
@RonakShah,x_fmonth1
输出错误,它只是一遍又一遍地重复同一列!你看到区别了吗?我认为关键是在函数中传递名称。使用map
时,我传递名称(称为.x
),而使用imap
时,名称称为.y
。您可以使用方法
和公式
参数规范的组合在geom_smooth()
中传递自定义曲线拟合。您有一个复杂的用例,我刚刚回复了您关于在ggplot
中绘制曲线函数的问题。这是可能的,但可能需要一些实验来满足您的确切需求。试一试,如果你被卡住了,再发一个单独的问题。
purrr::imap_dfc(hetro_var(m5), ~sigma(m5)^2*abs(dat$x)^(2*hetro_var(m5)[.y]))