比如:tempdata[i,'MEI']<-MEI[tempdata[i,'year'],tempdata[i,'month']] matrix[rname,cname]
比如:list.files()得到当前wd的所有文件名字
比如:sapply(tempdata$Date,function(x){strsplit(x,"-")[[1]][1]}) 根据指定分隔符位置划分字符串,如提取年月
比如:tapply(O18,list(group),function(x){mean(x,na.rm = T)}) 根据分组变量应用匿名函数,tapply()
#geology
#set the wd
setwd("C:/Users/Administrator/Desktop/mission/geology/data")
setwd("C:/Users/jack/Desktop/mission/correlation/geology/data")
#load the MEI
MEI <- as.data.frame(read_excel("C:/Users/Administrator/Desktop/mission/geology/MEI.xlsx"))
MEI <- as.data.frame(read_excel("C:/Users/jack/Desktop/mission/correlation/geology/MEI.xlsx"))
#reconstruct the MEI file
rownames(MEI) <- MEI$YEAR
MEI<-MEI[,-c(1)]
colnames(MEI)
rownames(MEI)
MEI['1950','01']
#load the names of excels
myfiles0 <- list.files()
#extract the names of the cities
library(stringr)
city_names<-c()
for ( nameexcel in myfiles0){
temp<-str_sub(nameexcel,1,str_locate(nameexcel,"\\.")[1]-1)
city_names<-c(city_names,temp)
}
####create the table
table1<-matrix(NA,nrow = 39,ncol = 11)
tempdata_all<-as.data.frame(matrix(NA,nrow=1,ncol=12))
names(tempdata_all)<-names(tempdata)
table3<-matrix(NA,nrow = 39,ncol = 5)
####loops
library(readxl)
for (i in 1:length(myfiles0)){
tempfile<-as.data.frame(read_excel(myfiles0[i]))
tempdata <- tempfile[c(4,6:8,13,17,25,24)]
tempdata$year<-sapply(tempdata$Date,function(x){strsplit(x,"-")[[1]][1]})
tempdata$month<-sapply(tempdata$Date,function(x){strsplit(x,"-")[[1]][2]})
for(j in 1:nrow(tempdata)){
tempdata[j,'MEI']<-MEI[tempdata[j,'year'],tempdata[j,'month']]
}
# tempdata$group[tempdata$MEI<0] <- -1
# tempdata$group[tempdata$MEI>0] <- 1
tempdata$group[tempdata$year==1972|tempdata$year==1973|tempdata$year==1982|tempdata$year==1983|tempdata$year==1997|tempdata$year==1998|tempdata$year==2014|tempdata$year==2015|tempdata$year==2016] <- -1
tempdata$group[tempdata$year!=1972 & tempdata$year!=1973 & tempdata$year!=1982 & tempdata$year!=198