Read multiple csv data and create new columns at one time

天涯浪子 提交于 2019-12-11 06:15:13

问题


I have a file and there are many csv data in it.
I want to read them and create new columns at one time and then combine to one datatable. I explain more here.

  1. Look at this pic:

  1. I want to create 2 new columns YEAR and MONTH based on the csv data title.
    ex. Take 201508 Sales Report(London) as an example. I want to create YEAR = 2015 and MONTH = 8.

  2. I don't know how to do but I can read them at one time without create new columns.

    my_read_data <- function(path){  
    data <- data.table::fread(path, header = T, strip.white = T, fill = T)  
    data <- data[data[[5]] != 0,]  
    data <- subset(data, select = c(-1,-7,-10,-12,-13,-14,-15,-17))  
    }  
    file.list <- dir(path = "//path/", pattern='\\.csv', full.names = T)  
    df.list <- lapply(file.list, my_read_data)  
    dt <- rbindlist(df.list)    
    

How to modify my code?
Actually I'm not sure whether my code is correct or not.
Appreciate.

Thanks to @Jaap, my new code is:

my_read_data <- function(x){
data <- data.table::fread(x, header = T, strip.white = T, fill = T)
data <- data[data[[5]] != 0,]
data <- subset(data, select = c(-1,-7,-10,-12,-13,-14,-15,-17))
}
file.list <- list.files(path = "/path/", pattern = '*.csv')
dt.list <- sapply(file.list, my_read_data, simplify=FALSE)

However, I get an error.

Error in data.table::fread(x, header = T, strip.white = T, fill = T) :   
File not found: C:\Users\PECHEN\AppData\Local\Temp\RtmpiihFR4\filea0c4d726488   

In addition: Warning messages:
1: running command 'C:\Windows\system32\cmd.exe /c (TWM-201508 Sales Report(London).csv) > C:\Users\PECHEN\AppData\Local\Temp\RtmpiihFR4\filea0c4d726488' had status 1 
2: In shell(paste("(", input, ") > ", tt, sep = "")) :
  '(TWM-201508 Sales Report(London).csv) > C:\Users\PECHEN\AppData\Local\Temp\RtmpiihFR4\filea0c4d726488' execution failed with error code 1  

Moreover, I edit my code:

my_read_data <- function(x){
data <- data.table::fread(x, header = T, strip.white = T, fill = T)
data <- data[data[[5]] != 0,]
data <- subset(data, select = c(-1,-7,-10,-12,-13,-14,-15,-17))
}
file.list <- dir(path = "/path/", pattern='\\.csv', full.names = T)  
df.list <- lapply(file.list, my_read_data)  
dt <- rbindlist(df.list, idcol = 'id')[, `:=` (YEAR = substr(id,5,8), MONTH = substr(id,9,10))]   

I use YEAR = substr(id,5,8), MONTH = substr(id,9,10) since each data title has four charater before numbers. ex. AAA-201508Sales Report
However, it doesn't work.
Thanks to @Peter TW, it works.


回答1:


Expanding on my comment and supposing that all the files have the same structure, the following should work:

library(data.table)
# get list of file-names
file.list <- list.files(pattern='*.csv')

# read the files with sapply & fread
# this will create a named list of data.tables
dt.list <- sapply(file.list, fread, simplify=FALSE)

# bind the list together to one data.table
# using the 'idcol'-parameter puts the names of the data.tables in the id-column
# create the YEAR & MONTH variables with 'substr'
DT <- rbindlist(dt.list, idcol = 'id')[, `:=` (YEAR = substr(id,1,4), MONTH = substr(id,5,6))]

This will result in one data.table with all the data and a YEAR and MONTH column added.

If you want to exclude certain columns from the files, you can use the drop-parameter of fread as follows:

dt.list <- sapply(file.list, fread, drop = c(1,7,10,12:15,17), simplify=FALSE)



回答2:


Here's how you can include the columns with dplyr:

nam <- c("201508 Sales Report(London)", "201509 Sales Report(London)", "201604 Sales Report(London)-Monthly")

dat <- data.frame(file=nam, var=nam)
dat %>% 
   separate(var, into=c(paste0("parts", 1:5))) %>% 
   mutate(Year=substring(parts1, 1,4), Month=substring(parts1, 5,6)) %>% 
   select(Year, Month, file)

#   Year Month                                file
# 1 2015    08         201508 Sales Report(London)
# 2 2015    09         201509 Sales Report(London)
# 3 2016    04 201604 Sales Report(London)-Monthly


来源:https://stackoverflow.com/questions/44302912/read-multiple-csv-data-and-create-new-columns-at-one-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!