vlambda博客
学习文章列表

R语言合并多个sheet表


记个小知识点:

R设置默认文件位置:

setwd('C:\\Users\\Administrator\\Desktop\\test.xlsx');

R查看默认文件位置:

getwd();

R提取Excel的sheet表数和sheet表名:

library readxl

length(readxl::excel_sheets('test.xlsx'))

names=readxl::excel_sheets('test.xlsx')


python查询默认路径:

import os

os.getcwd()

修改默认路径:

import os

os.chdir(r'C:\Users\laihuichun\desktop\test')


R读取一个excel文件,将sheet页数据取出来,并将sheet名字添加进去;

最后关联其他表格,输出需要的数据;

getwd() #setwd('C:\\Users\\Administrator\\Desktop\\test.xlsx')install.packages(openxlsx)#读取xlsx文件library(openxlsx)install.packages("readxl")#读取xlsx文件表长度,表名library(readxl)data=read_xlsx('test.xlsx')head(data)
#提取Excel的sheet表数和sheet表名length(readxl::excel_sheets('test.xlsx'))#sheet数量names=readxl::excel_sheets('test.xlsx')#表名names[1]#验证index<-1:length(readxl::excel_sheets('test.xlsx'))bind=list()#新增一列名称进来data=list()#新增数据for(i in index){ data[[i]]=read_xlsx('test.xlsx',sheet=i,n_max=16) bind[[i]]=readxl::excel_sheets('test.xlsx')[i] data[[i]]<-cbind(bind[[i]],data[[i]])#把日期绑定在前面}data#将数据合并在一起rbind<-c()for (i in 1:length(data)){ rbind<-rbind(rbind,data[[i]])}colnames(rbind)<-c('date','sid','name','id','score','ranks')rbind$date<-replace(rbind$date,'4.','2020/04/')complete.cases(rbind$date)rind<-unique(rbind)#替换日期格式成标准格式rind$date<-gsub('4.','2020/4/',rind$date)#排序rinds<-rind[order(rind$date,rind$ranks),];rinds#匹配表tousu<-read_xlsx('merge.xlsx');tousutousu<-as.data.frame(tousu);colnames(rinds)#通过merge连接merge<-merge(rinds,tousu,by=c('date','ranks'),all.x=TRUE,sort=TRUE);merge#只取某些列merge<-merge[,c(1,2,3,4,5,6,8)]#输出到表格write.csv(merge,'cbind.csv',sep=',')



R拆分合并表格可参考:

http://blog.sina.com.cn/s/blog_13ec1876a0102y4c5.html



修改anoconda浏览器文件位置:

C:\Users\laihuichun\.jupyter\jupyter_notebook_config.py ;  

网址https://blog.csdn.net/qq_43433255/article/details/93396747