我有一组csv文件,它们在所有文件中包含相同格式的数据。 格式如下
CA.csv --
row:1- jones
row:2- May
row:3-
row:4- Date_transaction cards_hold status amount
row:5- 5/25/2018 3:15 2 Active 233
row:6- 5/25/2018 3:30 2 Active 4534
NY.csv --
row:1- gary
row:2- May
row:3-
row:4- Date_transaction cards_hold status amount
row:5- 5/25/2018 5:00 2 Active 565
row:6- 5/25/2018 6:30 2 Active 533
以上格式会针对所有其他csv文件重复
o/p (i.e name is in first row in csv file should read and create as value in "name" column)
Date_transaction cards_hold status amount name
5/25/2018 3:15 2 Active 233 jones
5/25/2018 3:30 2 Active 4534 jones
5/25/2018 5:00 2 Active 565 gary
5/25/2018 6:30 2 Active 533 gary
我尝试使用以下代码。
files <- dir("RawData", recursive=TRUE, full.names=TRUE, pattern="\\.csv$")
raw = rbindlist(lapply(files, fread))
答案 0 :(得分:0)
希望这有帮助!
setwd("directory_path_of_your_csv_files/")
file_lst <- list.files(".")
read_all_csv <- function(f_name){
csv_data <- read.csv(f_name, header=T, stringsAsFactors=F, skip=3)
name <- read.csv(f_name, header=F, nrows=1)$V1
csv_data$name <- rep(name, nrow(csv_data))
csv_data
}
df <- Reduce(rbind, lapply(file_lst, function(x) read_all_csv(x)))