【发布时间】:2019-09-03 11:55:27
【问题描述】:
我在尝试基于列表循环函数并创建结果表时遇到了一些麻烦。
head(Dat17_18.3[,c(1,3:5)])
Program Area I.E.ScoreStat I.I.ScoreStat I.P.ScoreStat
1 BFA - Art Education 2 2 2
2 BA - Foreign Language Education 2 2 3
3 MAT - Elementary Education 2 2 2
4 BA - Dance Education 2 3 3
5 MAT - Elementary Education 2 3 3
6 MAT - Elementary Education 2 2 3
我已经使用split 函数创建了一个程序区域列表。
test1<- split(Dat17_18.3, Dat17_18.3$`Program Area`)
我编写了一个函数,用于从 psych 包中的 alpha 函数中提取元素。
alpha_fun <- function (df,columns){
library(psych)
a1 <- alpha(df[,columns])
a2 <- alpha.ci(a1[[1]][1],nrow(df),length(columns))
vec1 <- c(nrow(x),a1[[1]][1],a2[1],a2[3],a1[[1]][3])
return(vec1)
}
我想让 alpha_fun 为测试中的每个元素运行一次,以便为所有程序区域创建一个表。
【问题讨论】:
-
你的函数的参数是什么?哪一个是数据框,另一个是什么,它是从哪里得出的?建议使用更清晰的名称:
function(df, num)? -
函数的参数是x=df, y=col_numbers。
-
您有包裹限制吗?您是否愿意接受使用例如`dplyr
,data.table, orpurrr`的解决方案?还是您想主要留在基础 R 中? -
@Gregor,没有限制
标签: r function datatable sapply