I am trying to use the summarySE
function from package Rmisc
to generate data summaries for each column in a workbook. The first column in the worksheet is the grouping variable, and I want to loop through the other columns.
I am using the following code:
library(Rmisc)
for(i in 2:ncol(file)){
sum<-summarySE(file, measurevar = file[,i], groupvars = file[1])
}
But I keep getting the same error:
'Error in UseMethod("as.quoted") :
no applicable method for 'as.quoted' applied to an object of class
"data.frame"'
I know that file[1]
is a list
and should be a vector
, but using unlist
causes more problems. Any ideas?
Data:
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"))
We need a data.frame
to apply the summarySE
. Using lapply
, loop though the sequence of columns, subset the 'file', specify the measurevar
and groupvars
(based on the index of columns)
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
Or if we are using the OP's method
lst <- vector("list", ncol(file)-1)
for(i in 2:ncol(file)){
lst[[i]] <- summarySE(file, measurevar = i, groupvars = 1)
}
lst
Note that we can also specify the names
instead of index
for(i in 2:ncol(file)){
lst[[i]] <- summarySE(file, measurevar = names(file)[i], groupvars = names(file)[1])
}
In the OP's code, the measurevar
and groupvars
are taking the values of the columns instead of the column name.