Reputation: 15
I have many csv files and I'm trying to read by "data.table". The structure of csv files are same (3 column).
My goal is to read all csv file in one main dataframe! each csv file should be add as column not in row.
for example:
My main DF structure should be like is:
Name Name2 csv_file_name_1 csv_file_name_2 .......
Now, what I want to do is: read new csv files (I'm reading just column number 3) and add to the DF as column.
For example if I have 30 csv file I have to read all of them and add 3th columns as new column in main dataframe. (final DF should have 30 column)
I wrote some code, that I can read files and get a file name and everything is working well.
files = list.files(path = path, pattern=".*csv")
for (i in 1:length(files)) assign(str_sub(files[i],11,-5),
fread(files[i],colClasses = c("NULL", "NULL", NA)))
But how I can add as column to main DF? "assign" is creating new dataframe!
Upvotes: 0
Views: 669
Reputation: 2227
Have you tried cbind
?
Ideally, each CSV table would have a unique identifier that would allow you to merge by that identifier.
Side note, using cbind
, be careful to ensure rows in each data.frame correspond to the same observation (ie row one in df1 is for the same observation as row 1 in df2).
Upvotes: 0
Reputation: 521513
You might be able to achieve this using cbind()
, which can bind each new data frame you read to the existing one. I said might because this would only make good sense if you expected each CSV file you read to have the same number of rows. It should be clear to you why this is a requirement. With this caveat in mind, here is a code snippet which reads in two CSV files to give the type of output you want:
df <- read.csv(file="path/to/file1.csv", sep=",", header=TRUE)
df_new <- read.csv(file="path/to/file2.csv", sep=",", header=TRUE)
# this next is required only if you need to change the column names in each
# subsequent file to something else
names(df_new) <- c("Name2", "csv_file_name2", "other_column2")
df <- cbind(df, df_new)
Upvotes: 1