计算均值为R的列表数据

时间:2014-10-14 05:21:20

标签: r list mean standard-deviation

所以这就是我的问题:我有一些代码从29个excel文件中提取一些数据,并组织任何标记为" sw"变成一个变量,标记为" rep"进入另一个变量:

file.number <- c(1:29)
data <-setNames(lapply(paste0(file.number,".csv"),read.csv,stringsAsFactors=FALSE),paste0(file.number,".data"))
n <- 1:29
df <- data.frame(RT=1:100,rep.sw=sample(c("sw","rep",100,replace=TRUE)))
sw <- lapply(data[n],function(df) with(df,na.omit(RT[rep.sw=="sw"])))  
rep <- lapply(data[n],function(df) with(df,na.omit(RT[rep.sw=="rep"])))

然后我想找出这些文件的均值和标准偏差,除非我使用mean(sw)它告诉我&#34;参数不是数字或逻辑:返回NA&#34;。如果我打开&#34; sw&#34;或&#34; rep&#34;我得到这样的东西:

$ 28.data  [1] 0.8476 0.8362 0.5442 0.6987 0.7859 0.7396 1.0230 1.2446 0.8683 0.6049 0.6355 0.7421 0.9611 [14] 0.8074 0.9847 0.6291 1.0054 0.6969 0.7265 0.6452 0.7258 0.8099 0.6202 0.7873 0.6800 0.6932 [27] 1.4137 0.9585 1.6352 0.5182 0.9112 0.6410 0.7425 0.8477 0.6520 0.7538 1.0690 0.4945 0.6436 [40] 0.3885 0.6794 0.7635 0.7180 0.4817 0.6300 0.7036 0.6592 0.5893 0.7757 0.7562 0.9872 1.2523 [53] 0.6881 0.9567 1.2612 0.6691 1.0147 0.7342 0.5541 0.7812 0.8366 0.6086 0.3273 2.7230 1.1746 [66] 0.6796 0.5465 0.7613 0.7385 0.7043 0.6008 0.5958 1.1628 0.6029 0.6236 0.6968 0.9634 0.4779 [79] 0.7606 0.9773 0.7741 0.5647 0.8278 0.5899 0.5874 0.7234 0.5261 0.5980 0.5951 ATTR(&#34; na.action&#34)   [1] 1 3 4 5 7 9 10 12 14 16 18 20 22 24 26 27 29 31 33 35 37 39 41  [24] 42 44 46 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85  [47] 86 88 90 92 94 95 97 99 101 102 103 105 107 109 111 112 114 116 118 119 121 123 125  [70] 126 128 130 132 134 135 136 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167  [93] 169 171 173 174 176 178 179 181 183 185 187 189 191 193 195 ATTR(&#34;类&#34) [1]&#34;省略&#34;

$ 29.data  [1] 0.6426 0.5946 0.6903 0.6798 0.7299 0.8263 0.6023 0.6095 0.5822 0.7946 0.5403 0.5716 0.6120 [14] 0.6508 0.5124 0.4831 0.5197 0.5750 0.5578 0.5993 0.7203 0.5777 0.5906 0.4900 0.5620 0.7961 [27] 0.5908 0.6904 0.5560 0.4818 0.4990 0.5240 0.5225 0.6483 0.6777 0.4982 0.6693 0.6540 0.5528 [40] 0.5033 0.5314 0.5208 0.5375 0.6032 0.5255 0.5439 0.4703 0.5123 0.6321 0.5057 0.5668 0.5387 [53] 0.5980 0.5555 0.5745 0.5961 0.5186 0.5541 0.5976 0.9179 0.6375 0.5769 0.5807 0.6663 0.6177 [66] 0.5507 0.4639 0.8702 0.5002 0.5325 0.4975 0.5741 0.5051 0.5476 0.8307 0.4861 0.6348 0.5990 [79] 0.5649 0.6674 0.5247 0.4840 0.5376 0.5241 0.6764 0.5281 0.6310 0.6082 0.5833 0.5424 0.5984 [92] 0.6329 ATTR(&#34; na.action&#34)   [1] 1 3 5 7 9 11 12 14 16 18 20 21 23 24 25 27 28 30 32 34 36 38 39  [24] 41 43 45 47 49 50 52 53 54 56 58 60 62 63 65 67 69 70 72 73 74 76 78  [47] 79 81 82 84 85 87 89 91 93 95 97 99 101 102 104 106 108 109 111 112 114 116 118  [70] 120 122 123 125 127 129 131 133 135 137 139 141 143 145 146 148 150 152 153 155 157 159 161  [93] 163 164 166 167 168 170 172 174 175 177 179 180 182 184 186 187 189 191 193 195 197 199 201 [116] 203 205 206 208 210 212 213 ATTR(&#34;类&#34) [1]&#34;省略&#34;

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

您的swrep个对象是list个对象,因此您无法直接在其上使用mean

如果您想要列表中每个组件的平均值,您可以:

    sapply(sw,mean)
相关问题