-1

I have 10 csv files that have the same data.frame structure [75 x 1259]. They were imported in R using a list. I want to take the same columns in each csv file and create a new data.frame [75 X 11], so at the end I have 1259 data frames of 75 X 11 (11 columns, because the first column is the same in every data.frame so it can be used for the “by=” argument).

I used to have a code that can do this for 3 data.frame, which wasn’t very sophisticated.

Tab<-function(k){
  left_join(select(Ini1, c("value",k)), select(Aug2, c("value",k)), by="value") %>%
   left_join(., select(Dim2, c("value",k)), by="value") }

for (k in 2:1258) {
  write.csv(data.frame(Tab(k)), paste0('/Users/Tableau des features/',k,'.csv'), row.names = T)

I would love if I wasn’t obliged to do 7 more pipes. I read this Simultaneously merge multiple data.frames in a list to help me understand merging multiple data.frame. The code below sound good.

csv.list %>% reduce(left_join, by="value")

The only problem is how I select the columns I want to merge.

1 Answers1

0

If csv.list is a list of dataframe and the column value is present in all the dataframe then you can use Reduce function like this:

library(tidyverse)
## select same column from every dataframe 
new_csv_list <- lapply(csv.list, function(x) x %>% select(value, Image))
## Merge all the dataframe using the updated list
df <- Reduce(function(x,y) merge(x, y, by = 'value', all.x = TRUE), new_csv_list)
forhad
  • 196
  • 4
  • How do I specify wich columns to merge ? Exemple if I want c("value", "Image") only in every files. – Tchat Cusson Aug 10 '21 at 17:25
  • If all your dataframe column names are like `("value", "Image") ` then after merging it'll causes naming difficulty. You will see the column name like this `value, image.x, image.y, image.z, ....`. – forhad Aug 10 '21 at 17:43
  • I know it can be a problem in some projects, but in my case not really because x=observer 1, y= observer 2 ... and it easier to do ICC. – Tchat Cusson Aug 10 '21 at 17:48
  • Please see the updated answer if you want to achieve what you have mentioned. – forhad Aug 10 '21 at 17:48