R处理xls表格数据的探索

场景设定:处理一批气象数据,一年12月共12个xls文件,每个文件含当月天数*2个sheets,每个sheet包含前几行的固定说明以及从第6行开始的逐时气象数据,两批sheet各有相同的格式但数据指标不同,现解决如下问题:将其中的某些指标提取出来并按照时间顺序组成一年时间序列的数据,解决手段R,辅助包rJava,xlsxjars,xlsx。

代码如下:

#This R scripts are used to handle meteorological data
library(rJava)
library(xlsxjars)
library(xlsx)
#file1<-".../气象数据/"                          #Obtain root dir
#allfiles<-list.files(file1)                     #obtain all file names
THRAD<-c(1,1:2)
names(THRAD)<-c("Eg","PAR")
THLOG<-c(1,1:13)
names(THLOG)< c("T","Ee","P","Winds","Windd","D0","D5","D10","D15","D20","D40","D60","D100")
#January
JRfnames<-paste("Rad01",1:31,sep="")
JLfnames<-paste("Log01",1:31,sep="")
for(i in 1:31)
{
  tfile<-JRfnames[i]
  tfile1<-JRfnames[i]
  tfile<-read.xlsx(".../气象数据/01.xls",i,encoding="UTF-8",stringsAsFactors=FALSE)
  tfile1<-read.xlsx(".../气象数据/01.xls",i+31,encoding="UTF-8",stringsAsFactors=FALSE)
  Ntfile<-tfile[6:29,c(4,8)]
  names(Ntfile)<-c("Eg","PAR")                        #rename coloum names
  Ntfile1<-tfile1[6:29,c(3,12:13,24:25,36:43)]
  names(Ntfile1)<-c("T","Ee","P","Winds","Windd","D0","D5","D10","D15","D20","D40","D60","D100")
  THRAD<-rbind(THRAD,Ntfile,stringsAsFactors=FALSE)
  THLOG<-rbind(THLOG,Ntfile1,stringsAsFactors=FALSE)
}
...
write.csv(THRAD,".../Rad.csv")
write.csv(THLOG,".../Log.csv")

此次探索过程中主要解决了两个问题:(1)rbind()在使用的过程中一定要保证列名一致,否则会出现错误,可以采用names()重命名列名后再进行纵向合并;(2)rbind()在拼接的过程中可能出现因子问题,此处使用stringsAsFaxctors=FALSE规避这个问题。

你可能感兴趣的:(R,expolring)