R语言 matrix 列名和行名 可以直接mapping数字到另一个数据框

比如: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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值