使用R软件,解锁数据读取新姿势。
1.批量读取文件夹里面某类文件
library(data.table)
file_names<- list.files(pattern = ".csv")
for (i in 1:length(file_names)) {
name<-gsub(".csv","",file_names[i])
assign(name,fread(file_names[i])
}
2.批量将文件夹某类文件读取进来并合并成一个数据框,要求数据规整
file <- list.files(pattern = ".csv")#正则表达式匹配文件后缀
names(file) <- files
data <- lapply(file, fread)
data0 <- do.call(rbind,data)
3.批量读取Excel中多个Sheet工作表合并到一个数据框里,同样要求数据规整。
install.packages("openxlsx")
library(openxlsx)
aa <-NA
pb <- txtProgressBar(min = 0, max =2, style = 3)
for (i in 1:2){
mydata<-read.xlsx("###.xlsx", sheet = i, startRow = 1, colNames = T,
rowNames = FALSE, detectDates = FALSE, skipEmptyRows = TRUE,
skipEmptyCols = TRUE, rows = NULL, cols = NULL, check.names = FALSE,
namedRegion = NULL, na.strings = "NA", fillMergedCells = FALSE)
aa <- rbind(aa,mydata)
setTxtProgressBar(pb,i)
}
4.批量写出全局环境中的数据
for (i in 1:length(ls()))
{write.csv(eval(parse(text = ls(pattern = " ")[i])),
paste0(),na="",row.names=F)# paste0()定义文件名
}
课程分享
生信技能树全球公益巡讲
(https://mp.weixin.qq.com/s/E9ykuIbc-2Ja9HOY0bn_6g)
B站公益74小时生信工程师教学视频合辑
(https://mp.weixin.qq.com/s/IyFK7l_WBAiUgqQi8O7Hxw)
招学徒:
(https://mp.weixin.qq.com/s/KgbilzXnFjbKKunuw7NVfw)
网友评论