Arindam Arindam - 1 year ago 72
R Question

Combining columns from different files into a single column in a new file in R

I have extracted chosen columns from multiple files into new as many data sets as files. Now I need to rbind all of them into a single column in a new file.

I am using below command

comments <- bind_rows(data1,data2,data3,data4,data5,data6,data7)

Each of the data set has a single column but colname is different and I want all of them in a single column in comments data set.

The output I get is comments dataset with 7 columns instead of 1, struggling to understand why. Pls help

Answer Source

We can change the column names all at once in a list and then apply bind_rows

lapply(mget(paste0("data", 1:7)), setNames, "Col1") %>% 

Or use rbindlist from data.table which will give a single column

rbindlist(mget(paste0("data", 1:7))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download