R: Merging data frames based on file name The 2019 Stack Overflow Developer Survey Results Are InHow to join (merge) data frames (inner, outer, left, right)R - list to data frameDrop data frame columns by nameIs it possible to force Excel recognize UTF-8 CSV files automatically?Changing column names of a data frameWhy is `[` better than `subset`?data.table vs dplyr: can one do something well the other can't or does poorly?How to merge subsets of data frames from a list (i.e., merge all of the first dfs from each list component)Adding name of individual data frame to new column in merged data frameImporting and merging .csv files with loop in R

What to do when moving next to a bird sanctuary with a loosely-domesticated cat?

Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?

How do I free up internal storage if I don't have any apps downloaded?

Deal with toxic manager when you can't quit

Why doesn't UInt have a toDouble()?

Geography at the pixel level

Why are there uneven bright areas in this photo of black hole?

Can I have a signal generator on while it's not connected?

How to support a colleague who finds meetings extremely tiring?

Output the Arecibo Message

How do you keep chess fun when your opponent constantly beats you?

Correct punctuation for showing a character's confusion

What is this business jet?

Why can't devices on different VLANs, but on the same subnet, communicate?

Old scifi movie from the 50s or 60s with men in solid red uniforms who interrogate a spy from the past

How to obtain a position of last non-zero element

Pokemon Turn Based battle (Python)

Mathematics of imaging the black hole

Can withdrawing asylum be illegal?

A female thief is not sold to make restitution -- so what happens instead?

Are spiders unable to hurt humans, especially very small spiders?

Match Roman Numerals

For what reasons would an animal species NOT cross a *horizontal* land bridge?

What is the meaning of Triage in Cybersec world?



R: Merging data frames based on file name



The 2019 Stack Overflow Developer Survey Results Are InHow to join (merge) data frames (inner, outer, left, right)R - list to data frameDrop data frame columns by nameIs it possible to force Excel recognize UTF-8 CSV files automatically?Changing column names of a data frameWhy is `[` better than `subset`?data.table vs dplyr: can one do something well the other can't or does poorly?How to merge subsets of data frames from a list (i.e., merge all of the first dfs from each list component)Adding name of individual data frame to new column in merged data frameImporting and merging .csv files with loop in R



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















My files (example as I have hundreds of these files):



France.csv
France_variables.csv
Germany.csv
Germany_variables.csv
Spain.csv
Spain_variables.csv
Portugal.csv
Portugal_variables.csv


I want to merge France with France_variables, Germany with Germany_variables etc. I know I can use rbind with the two files but I want to do this as a loop because I have lots of these to merge. I'm not sure how to do a string search and then rbind in a loop or if there is a better way of doing this.



I am new to R so any help would be much appreciated.










share|improve this question






















  • What do you mean by merge?

    – Sonny
    Mar 8 at 11:18











  • add the data in France.csv to France_variables.csv

    – user11170440
    Mar 8 at 11:20











  • Do you want different df from each country? or you want all to be merged into single df. Are the headers same or different?

    – Sonny
    Mar 8 at 11:22











  • A different df for each country

    – user11170440
    Mar 8 at 11:23











  • In terms of approach, I'd say the way to start is to figure out how to put each set into a list, e.g. list(France,csv, Germany, csv,...) and list(France_variables.csv, Germany_variables.csv,...). Then I'd use either a for loop or lapply() to merge them, where all the outputs would be in a list

    – cyun
    Mar 8 at 11:38

















0















My files (example as I have hundreds of these files):



France.csv
France_variables.csv
Germany.csv
Germany_variables.csv
Spain.csv
Spain_variables.csv
Portugal.csv
Portugal_variables.csv


I want to merge France with France_variables, Germany with Germany_variables etc. I know I can use rbind with the two files but I want to do this as a loop because I have lots of these to merge. I'm not sure how to do a string search and then rbind in a loop or if there is a better way of doing this.



I am new to R so any help would be much appreciated.










share|improve this question






















  • What do you mean by merge?

    – Sonny
    Mar 8 at 11:18











  • add the data in France.csv to France_variables.csv

    – user11170440
    Mar 8 at 11:20











  • Do you want different df from each country? or you want all to be merged into single df. Are the headers same or different?

    – Sonny
    Mar 8 at 11:22











  • A different df for each country

    – user11170440
    Mar 8 at 11:23











  • In terms of approach, I'd say the way to start is to figure out how to put each set into a list, e.g. list(France,csv, Germany, csv,...) and list(France_variables.csv, Germany_variables.csv,...). Then I'd use either a for loop or lapply() to merge them, where all the outputs would be in a list

    – cyun
    Mar 8 at 11:38













0












0








0








My files (example as I have hundreds of these files):



France.csv
France_variables.csv
Germany.csv
Germany_variables.csv
Spain.csv
Spain_variables.csv
Portugal.csv
Portugal_variables.csv


I want to merge France with France_variables, Germany with Germany_variables etc. I know I can use rbind with the two files but I want to do this as a loop because I have lots of these to merge. I'm not sure how to do a string search and then rbind in a loop or if there is a better way of doing this.



I am new to R so any help would be much appreciated.










share|improve this question














My files (example as I have hundreds of these files):



France.csv
France_variables.csv
Germany.csv
Germany_variables.csv
Spain.csv
Spain_variables.csv
Portugal.csv
Portugal_variables.csv


I want to merge France with France_variables, Germany with Germany_variables etc. I know I can use rbind with the two files but I want to do this as a loop because I have lots of these to merge. I'm not sure how to do a string search and then rbind in a loop or if there is a better way of doing this.



I am new to R so any help would be much appreciated.







r csv






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 11:16









user11170440user11170440

1




1












  • What do you mean by merge?

    – Sonny
    Mar 8 at 11:18











  • add the data in France.csv to France_variables.csv

    – user11170440
    Mar 8 at 11:20











  • Do you want different df from each country? or you want all to be merged into single df. Are the headers same or different?

    – Sonny
    Mar 8 at 11:22











  • A different df for each country

    – user11170440
    Mar 8 at 11:23











  • In terms of approach, I'd say the way to start is to figure out how to put each set into a list, e.g. list(France,csv, Germany, csv,...) and list(France_variables.csv, Germany_variables.csv,...). Then I'd use either a for loop or lapply() to merge them, where all the outputs would be in a list

    – cyun
    Mar 8 at 11:38

















  • What do you mean by merge?

    – Sonny
    Mar 8 at 11:18











  • add the data in France.csv to France_variables.csv

    – user11170440
    Mar 8 at 11:20











  • Do you want different df from each country? or you want all to be merged into single df. Are the headers same or different?

    – Sonny
    Mar 8 at 11:22











  • A different df for each country

    – user11170440
    Mar 8 at 11:23











  • In terms of approach, I'd say the way to start is to figure out how to put each set into a list, e.g. list(France,csv, Germany, csv,...) and list(France_variables.csv, Germany_variables.csv,...). Then I'd use either a for loop or lapply() to merge them, where all the outputs would be in a list

    – cyun
    Mar 8 at 11:38
















What do you mean by merge?

– Sonny
Mar 8 at 11:18





What do you mean by merge?

– Sonny
Mar 8 at 11:18













add the data in France.csv to France_variables.csv

– user11170440
Mar 8 at 11:20





add the data in France.csv to France_variables.csv

– user11170440
Mar 8 at 11:20













Do you want different df from each country? or you want all to be merged into single df. Are the headers same or different?

– Sonny
Mar 8 at 11:22





Do you want different df from each country? or you want all to be merged into single df. Are the headers same or different?

– Sonny
Mar 8 at 11:22













A different df for each country

– user11170440
Mar 8 at 11:23





A different df for each country

– user11170440
Mar 8 at 11:23













In terms of approach, I'd say the way to start is to figure out how to put each set into a list, e.g. list(France,csv, Germany, csv,...) and list(France_variables.csv, Germany_variables.csv,...). Then I'd use either a for loop or lapply() to merge them, where all the outputs would be in a list

– cyun
Mar 8 at 11:38





In terms of approach, I'd say the way to start is to figure out how to put each set into a list, e.g. list(France,csv, Germany, csv,...) and list(France_variables.csv, Germany_variables.csv,...). Then I'd use either a for loop or lapply() to merge them, where all the outputs would be in a list

– cyun
Mar 8 at 11:38












2 Answers
2






active

oldest

votes


















0














You can use something like this:



library(tidyverse)
#Get Unique countries
country <- unique(gsub('\..*$|_.*', '', list.files(path = ".", pattern = "csv")))

#Loop
for (i in country)
dat <- list.files(path = ".", pattern = i) %>% map(read_csv) %>% reduce(rbind)
assign( paste("df", i, sep = "_"), dat)
rm(dat)



This will create dataframes like df_France, df_Germany , etc.






share|improve this answer

























  • Thanks I'll try this

    – user11170440
    Mar 8 at 12:06


















0














Play with the 'grepl', and see if you can get this to work......



# set the working directory (where files are saved)
setwd("C:/your_path_here/")

file_names = list.files(getwd())
file_names = file_names[grepl(".TXT",file_names)]

# print file_names vector
file_names

# see the data structure
str(file)

# run read.csv on all values of file_names
files = lapply(file_names, read.csv, header=F, stringsAsFactors = F)
files = do.call(rbind,files)

# run only on WY.TXT and NM.TXT
str(files)


# set column names
names(files) = c("col1", "col2", "col3", "col4", "col5")
str(files)


# finally...
write.table(files, "C:/your_path/mydata.txt", sep="t")
write.csv(files,"C:/your_path/mydata.csv")


http://www.rforexcelusers.com/combine-delimited-files-r/






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55062108%2fr-merging-data-frames-based-on-file-name%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can use something like this:



    library(tidyverse)
    #Get Unique countries
    country <- unique(gsub('\..*$|_.*', '', list.files(path = ".", pattern = "csv")))

    #Loop
    for (i in country)
    dat <- list.files(path = ".", pattern = i) %>% map(read_csv) %>% reduce(rbind)
    assign( paste("df", i, sep = "_"), dat)
    rm(dat)



    This will create dataframes like df_France, df_Germany , etc.






    share|improve this answer

























    • Thanks I'll try this

      – user11170440
      Mar 8 at 12:06















    0














    You can use something like this:



    library(tidyverse)
    #Get Unique countries
    country <- unique(gsub('\..*$|_.*', '', list.files(path = ".", pattern = "csv")))

    #Loop
    for (i in country)
    dat <- list.files(path = ".", pattern = i) %>% map(read_csv) %>% reduce(rbind)
    assign( paste("df", i, sep = "_"), dat)
    rm(dat)



    This will create dataframes like df_France, df_Germany , etc.






    share|improve this answer

























    • Thanks I'll try this

      – user11170440
      Mar 8 at 12:06













    0












    0








    0







    You can use something like this:



    library(tidyverse)
    #Get Unique countries
    country <- unique(gsub('\..*$|_.*', '', list.files(path = ".", pattern = "csv")))

    #Loop
    for (i in country)
    dat <- list.files(path = ".", pattern = i) %>% map(read_csv) %>% reduce(rbind)
    assign( paste("df", i, sep = "_"), dat)
    rm(dat)



    This will create dataframes like df_France, df_Germany , etc.






    share|improve this answer















    You can use something like this:



    library(tidyverse)
    #Get Unique countries
    country <- unique(gsub('\..*$|_.*', '', list.files(path = ".", pattern = "csv")))

    #Loop
    for (i in country)
    dat <- list.files(path = ".", pattern = i) %>% map(read_csv) %>% reduce(rbind)
    assign( paste("df", i, sep = "_"), dat)
    rm(dat)



    This will create dataframes like df_France, df_Germany , etc.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 8 at 11:47

























    answered Mar 8 at 11:40









    SonnySonny

    2,0361515




    2,0361515












    • Thanks I'll try this

      – user11170440
      Mar 8 at 12:06

















    • Thanks I'll try this

      – user11170440
      Mar 8 at 12:06
















    Thanks I'll try this

    – user11170440
    Mar 8 at 12:06





    Thanks I'll try this

    – user11170440
    Mar 8 at 12:06













    0














    Play with the 'grepl', and see if you can get this to work......



    # set the working directory (where files are saved)
    setwd("C:/your_path_here/")

    file_names = list.files(getwd())
    file_names = file_names[grepl(".TXT",file_names)]

    # print file_names vector
    file_names

    # see the data structure
    str(file)

    # run read.csv on all values of file_names
    files = lapply(file_names, read.csv, header=F, stringsAsFactors = F)
    files = do.call(rbind,files)

    # run only on WY.TXT and NM.TXT
    str(files)


    # set column names
    names(files) = c("col1", "col2", "col3", "col4", "col5")
    str(files)


    # finally...
    write.table(files, "C:/your_path/mydata.txt", sep="t")
    write.csv(files,"C:/your_path/mydata.csv")


    http://www.rforexcelusers.com/combine-delimited-files-r/






    share|improve this answer



























      0














      Play with the 'grepl', and see if you can get this to work......



      # set the working directory (where files are saved)
      setwd("C:/your_path_here/")

      file_names = list.files(getwd())
      file_names = file_names[grepl(".TXT",file_names)]

      # print file_names vector
      file_names

      # see the data structure
      str(file)

      # run read.csv on all values of file_names
      files = lapply(file_names, read.csv, header=F, stringsAsFactors = F)
      files = do.call(rbind,files)

      # run only on WY.TXT and NM.TXT
      str(files)


      # set column names
      names(files) = c("col1", "col2", "col3", "col4", "col5")
      str(files)


      # finally...
      write.table(files, "C:/your_path/mydata.txt", sep="t")
      write.csv(files,"C:/your_path/mydata.csv")


      http://www.rforexcelusers.com/combine-delimited-files-r/






      share|improve this answer

























        0












        0








        0







        Play with the 'grepl', and see if you can get this to work......



        # set the working directory (where files are saved)
        setwd("C:/your_path_here/")

        file_names = list.files(getwd())
        file_names = file_names[grepl(".TXT",file_names)]

        # print file_names vector
        file_names

        # see the data structure
        str(file)

        # run read.csv on all values of file_names
        files = lapply(file_names, read.csv, header=F, stringsAsFactors = F)
        files = do.call(rbind,files)

        # run only on WY.TXT and NM.TXT
        str(files)


        # set column names
        names(files) = c("col1", "col2", "col3", "col4", "col5")
        str(files)


        # finally...
        write.table(files, "C:/your_path/mydata.txt", sep="t")
        write.csv(files,"C:/your_path/mydata.csv")


        http://www.rforexcelusers.com/combine-delimited-files-r/






        share|improve this answer













        Play with the 'grepl', and see if you can get this to work......



        # set the working directory (where files are saved)
        setwd("C:/your_path_here/")

        file_names = list.files(getwd())
        file_names = file_names[grepl(".TXT",file_names)]

        # print file_names vector
        file_names

        # see the data structure
        str(file)

        # run read.csv on all values of file_names
        files = lapply(file_names, read.csv, header=F, stringsAsFactors = F)
        files = do.call(rbind,files)

        # run only on WY.TXT and NM.TXT
        str(files)


        # set column names
        names(files) = c("col1", "col2", "col3", "col4", "col5")
        str(files)


        # finally...
        write.table(files, "C:/your_path/mydata.txt", sep="t")
        write.csv(files,"C:/your_path/mydata.csv")


        http://www.rforexcelusers.com/combine-delimited-files-r/







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 12 at 21:30









        ryguy72ryguy72

        4,6611825




        4,6611825



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55062108%2fr-merging-data-frames-based-on-file-name%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко