gpt4 book ai didi

r - 为什么 pivot_longer() 在其输出中只嵌套了四个整数值

转载 作者:行者123 更新时间:2023-12-04 14:39:09 31 4
gpt4 key购买 nike

我想知道为什么我的 pivot_longer()下面的调用返回 value 下的嵌套输出柱子?

foo <- function(){ 
n_subj = 5
n_trials = 20
subj_intercepts = rnorm(n_subj, 0, 1)
slope = .6
mx = 30
data = data.frame(subject = rep(1:n_subj, each=n_trials),
intercept = rep(subj_intercepts, each=n_trials)) %>%
mutate(x = rnorm(n(), mx, 1),
y = intercept + slope*(x-mean(x)) + rnorm(n(), 0, 1))

mlm = coef(summary(lmer(y ~ x + (1|subject), data=data)))[2,1]
ols = coef(summary(lm(y ~ x, data=data)))[2,1]
list(ols=ols, mlm=mlm)
}

kk <- data.frame(t(replicate(2, foo())))

pivot_longer(kk, everything())

# name value
# <chr> <list>
#1 ols <dbl [1]>
#2 mlm <dbl [1]>
#3 ols <dbl [1]>
#4 mlm <dbl [1]>

最佳答案

您正在函数中创建一个列表:Add/Wrap unlist在你的函数代码的末尾得到一个 tibble :

foo <- function(){ 
n_subj = 5
n_trials = 20
subj_intercepts = rnorm(n_subj, 0, 1)
slope = .6
mx = 30
data = data.frame(subject = rep(1:n_subj, each=n_trials),
intercept = rep(subj_intercepts, each=n_trials)) %>%
mutate(x = rnorm(n(), mx, 1),
y = intercept + slope*(x-mean(x)) + rnorm(n(), 0, 1))

mlm = coef(summary(lmer(y ~ x + (1|subject), data=data)))[2,1]
ols = coef(summary(lm(y ~ x, data=data)))[2,1]
unlist(list(ols=ols, mlm=mlm))
}
输出
  name  value
<chr> <dbl>
1 ols 0.600
2 mlm 0.581
3 ols 0.441
4 mlm 0.528

关于r - 为什么 pivot_longer() 在其输出中只嵌套了四个整数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69125259/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com