首先批量为每个文件加列名称
a = list.files()
dir = paste("./",a,sep="")
n = length(dir)
for (i in 1:n){
new_data = read.csv(file = dir[i],sep=";",header=F)
colnames(new_data)=c("genus","tpm")
write.table(new_data,a[i],sep = ";",quote = F,col.names = T,row.names = F)
}
使用dplyr包的full_join函数进行Merge
full_join函数可使用R base里的merge函数进行替换
merge_data = read.csv(file = dir[1],sep=";",header=T)
for (i in 2:n){
new_data = read.csv(file = dir[i],sep=";",header=T)
merge_data = full_join(merge_data,new_data,by = "genus")
}
替换R中默认的NA为0
merge_data[is.na(merge_data)]=0
写入文件,;进行分割,去掉“”,行名列名称不使用
write.table(merge_data,"merge_1_50",sep = ";",quote = F,col.names = F,row.names = F)
网友评论