0

How can I rbind all .RData dataframes which are stored in the same folder with identical column structure? I tried:

my_list <- list.files(my_path, full.names=TRUE)
my_files <- lapply(my_list, load, envir=.GlobalEnv)
library(dplyr)
df <- bind_rows(my_files, .id = "column_label")
  • 1
    what is the issue or the error message? it would also be convenient to provide at least 2-3 example structures of your dataframes. Just the head or the columns and data types. – Stephan May 05 '20 at 07:47
  • Hello :) have you tryied ideas mentionned in [this answer](https://stackoverflow.com/a/32841176/10264278)? – Paul May 05 '20 at 07:53
  • The error message is: Error: "Argument 1 must have names" – user13472708 May 05 '20 at 07:57
  • @ Paul: when I try ```data <- rbindlist(lapply(filenames,fread))``` I get: Error in FUN(X[[i]], ...) : embedded nul in string: '˜ïÚ>ùî\f ¾§\0177t.pÈô\016\002ºÔØ!_.zÝ3Ã\037\016\036¿\024ägº:Øñ}9ôÃ|«ƒbºÿ›°«\027HU\017÷—\\àÃG»§\024:Øx7'TW½upÚ±€#Å‘ÇAe§JM\034ƒ¥ƒ²K߆G²¼\016\022R‚k\031-\003ìŸê\034j!+\0Ô÷rR\016«ŸƒÝŽ"ïoÍO\034œž®9q¨d‹ƒ\\\030»KcÊN ¾9¿ÄÅjúXVny\\mÿT—c\025_Ót\aÐÓzÝ\033Íì¿­_»½ºBÔÁašîƒ:=+\aï\035i_X—š;\b9Ü9slã.û¯Êâ§\016ô1“ª\036©Ý\035©SÖ“EÏb*%‡î\flZE+\032è/\001pÁ\003I¡Œ†ÃŸ\001‹Éù\016\f' – user13472708 May 05 '20 at 08:25

1 Answers1

1

You can try :

lapply(my_files, load, .GlobalEnv)
df <- do.call(rbind, mget(sub('\\.RData', '', basename(my_files))))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thank you. When I run this I get a matrix with only one column which contains the names of the files. [,1] [1,] "CLC_1_all_300" [2,] "CLC_10_all_300" [3,] "CLC_100_all_300" [4,] "CLC_102_all_300" [5,] "CLC_106_all_300" [6,] "CLC_107_all_300" [7,] "CLC_109_all_300" – user13472708 May 05 '20 at 08:36
  • Thank you. There seem to be a conflict with the names of the saved RData dataframes. – user13472708 May 05 '20 at 11:48
  • @user13472708 I am not sure if I understand. It worked for me on small subset of `.RData` files that I had. Did it work for you? – Ronak Shah May 05 '20 at 11:54
  • Thank you. There seems to be a conflict with the names of the saved RData-dataframes. I had renamed the filenames, but the files which are loaded into the global environment still have the old names. I tried: ```my_list <- list.files(my_path, full.names=TRUE) my_files <- lapply(my_list, load, envir=.GlobalEnv) df <- do.call(rbind, mget(sub('\\.RData', '', basename(my_files))))``` but a file was not found. – user13472708 May 05 '20 at 11:58
  • Are `my_files` all unique? Or they have some overlapping names? – Ronak Shah May 05 '20 at 13:04