gpt4 book ai didi

r - 带for循环的数据汇总

转载 作者:行者123 更新时间:2023-12-04 12:11:44 32 4
gpt4 key购买 nike

我正在尝试使用 Rmisc 包中的 summarySE 函数为工作簿中的每一列生成数据摘要。工作表中的第一列是分组变量,我想遍历其他列。我正在使用以下代码:

library(Rmisc)
for(i in 2:ncol(file)){
sum<-summarySE(file, measurevar = file[,i], groupvars = file[1])
}

但我不断收到同样的错误:

'Error in UseMethod("as.quoted") : 
no applicable method for 'as.quoted' applied to an object of class
"data.frame"'

我知道 file[1] 是一个 list 并且应该是一个 vector,但是使用 unlist导致更多的问题。有什么想法吗?

数据:

structure(list(Treatment = c("SKELE", "SKELE", "SKELE", "SKELE", 
"SKELE", "SKELE", "SKELE", "SKELE", "SKELE", "SKELE", "SKELE",
"SKELE", "TISSUE", "TISSUE", "TISSUE", "TISSUE", "TISSUE", "TISSUE",
"TISSUE", "TISSUE", "TISSUE", "TISSUE", "TISSUE", "TISSUE"),
`% lipid in skeleton` = c(21.8706902567934, 31.1736436075643,
62.2246234617322, 86.6248675033794, 46.5607971373041, 34.7532319115317,
32.7686161366371, 6.73685660233744, 33.7111477556584, 48.8970450055359,
54.3687328279357, 48.9086732773318, 78.1293097432066, 68.8263563924357,
37.7753765382678, 13.3751324966206, 53.4392028626959, 65.2467680884683,
67.2313838633629, 93.2631433976626, 66.2888522443416, 51.1029549944641,
45.6312671720643, 51.0913267226682), `% ash in skeleton` = c(97.370981485225,
98.6169174273543, 99.2417548180554, 99.1330769035889, 98.5523872323069,
98.0077944962001, 97.7848485294277, 98.0738823145836, 98.1567971208113,
98.8047064451889, 97.1790753033603, 98.7503991978965, 2.62901851477497,
1.38308257264571, 0.75824518194458, 0.866923096411125, 1.44761276769314,
1.99220550379987, 2.2151514705723, 1.92611768541643, 1.84320287918869,
1.19529355481109, 2.82092469663973, 1.24960080210352), `% tissue in skeleton` = c(55.2224357342865,
70.022864703591, 77.5880978578982, 83.1168129092154, 67.3012504898307,
62.1455896726595, 64.2488985210074, 67.3089347382539, 59.9276126303114,
70.5681668501146, 67.717146912379, 68.8185249866557, 44.7775642657135,
29.977135296409, 22.4119021421018, 16.8831870907846, 32.6987495101694,
37.8544103273405, 35.7511014789926, 32.6910652617461, 40.0723873696886,
29.4318331498854, 32.2828530876211, 31.1814750133443)), class = "data.frame", row.names = c(NA,
-24L), .Names = c("Treatment", "% lipid in skeleton", "% ash in skeleton",
"% tissue in skeleton"))

最佳答案

我们需要一个data.frame 来应用summarySE。使用 lapply,遍历列序列,子集"file",指定 measurevargroupvars(基于列的索引)

lapply(2:ncol(file), function(j) summarySE(file[c(1, j)], measurevar = 2, groupvars = 1))
#[[1]]
# Treatment N 2 sd se ci
#1 SKELE 12 42.38324 20.53836 5.928913 13.04945
#2 TISSUE 12 57.61676 20.53836 5.928913 13.04945

#[[2]]
# Treatment N 2 sd se ci
#1 SKELE 12 98.306052 0.6567242 0.1895799 0.4172626
#2 TISSUE 12 1.693948 0.6567242 0.1895799 0.4172626

#[[3]]
# Treatment N 2 sd se ci
#1 SKELE 12 67.83219 7.442443 2.148448 4.728703
#2 TISSUE 12 32.16781 7.442443 2.148448 4.728703

或者如果我们使用 OP 的方法

lst <- vector("list", ncol(file)-1)
for(i in 2:ncol(file)){
lst[[i]] <- summarySE(file, measurevar = i, groupvars = 1)
}

lst

请注意,我们还可以指定 names 而不是 index

for(i in 2:ncol(file)){
lst[[i]] <- summarySE(file, measurevar = names(file)[i], groupvars = names(file)[1])
}

在 OP 的代码中,measurevargroupvars 使用列的值而不是列名。

关于r - 带for循环的数据汇总,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39564749/

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