2

所以我有这个名为'Birth.xls'. 我在这个电子表格中有 10 张表格,格式如下:

Sheet1
Number  Value 
3   0.125
6   0.2
9   0.275
12  0.35
15  0.425
17  0.5
19  0.575
21  0.65
23  0.725
25  0.8
27  0.875
29  0.95
31  1.025
Sheet2
Number  Value
3   0.614
6   0.654
9   0.694
12  0.734
15  0.774
17  0.814
19  0.854
21  0.894
23  0.934
25  0.974
27  1.014
29  1.054
31  1.094

我将如何在此电子表格中创建另一个“表格”,其中包含我上面使用 R 给出的表格的行平均值?...我知道我可以在 excel 中执行此操作,但由于有时我有 10 张或更多表格,它可以很痛苦……尤其是当我必须做比平均水平更复杂的事情时……所以总而言之,我想要一个类似于下面的输出

Sheet3
Number  Average Value
3   0.3695
6   0.427
9   0.4845
12  0.542
15  0.5995
17  0.657
19  0.7145
21  0.772
23  0.8295
25  0.887
27  0.9445
29  1.002
31  1.0595
4

1 回答 1

3

Here is a way I would do it. First, load all data, make them as one data frame, calculate means and then export to new sheet.

library(XLConnect)

#load xls file
wb<-loadWorkbook("Birth.xls")

#get sheet names of file
lp<-getSheets(wb)

#load each sheet in seperate list element
dat<-lapply(seq_along(lp),function(i) readWorksheet(wb,sheet=lp[i]))

#convert all data to one data frame by merging individual data frame
dat2<-Reduce(function(...) merge(..., by="Number"), dat)

#calculate row means  of merged data frames (-1 as first column contains Number)
dat2$res<-rowMeans(dat2[,-1])

#create new sheet to export data
createSheet(wb, name = "together")

#write calculated data to new sheet
writeWorksheet(wb,dat2[,c("Number","res")],sheet="together")

#save workbook
saveWorkbook(wb)
于 2013-03-31T08:51:30.737 回答