Assign the results of do.call using cbind to data frames












1















I want to combine multiple sets of two data frames (a & a_1, b & b_1, etc.). Basically, I want to do what this question is asking. I created a list of my two data sets:



# create data 
a <- c(1, 2, 3)
b <- c(2, 3, 4)
at0H0 <- data.frame(a, b)

c <- c(1, 2, 3)
d <- c(2, 3, 4)
at0H0_1 <- data.frame(c, d)

e <- c(1, 2, 3)
f <- c(2, 3, 4)
at0H1 <- data.frame(a, b)

g <- c(1, 2, 3)
h <- c(2, 3, 4)
at0H1_1 <- data.frame(c, d)

# create lists of names
names <- list("at0H0", "at0H1")
namesLPC <- list("at0H0_1", "at0H1_1")

# column bind the data frames?
dfList <- list(cbind(names, namesLPC))
do.call(cbind, dfList)


But now I need it to create data frames for each. This do.call function just creates a list of the names of the data frames. Thanks!



(Edited to make reproducible code)










share|improve this question




















  • 1





    The example code is not reproducible and ls is a bad choice for a variable name because of the ls() function. - Try to clarify your questions.

    – Florian
    Nov 20 '18 at 0:19






  • 1





    do.call(cbind, mget(unlist(names))) for instance should do it with your full example.

    – thelatemail
    Nov 20 '18 at 0:47











  • thanks @thelatemail, but I need them to be separate data frames. For example, at0H0 and at0H0_1 need to be in one data frame and at0H1 and at0H1_1 need to be in a separate data frame

    – Lisa
    Nov 20 '18 at 0:57
















1















I want to combine multiple sets of two data frames (a & a_1, b & b_1, etc.). Basically, I want to do what this question is asking. I created a list of my two data sets:



# create data 
a <- c(1, 2, 3)
b <- c(2, 3, 4)
at0H0 <- data.frame(a, b)

c <- c(1, 2, 3)
d <- c(2, 3, 4)
at0H0_1 <- data.frame(c, d)

e <- c(1, 2, 3)
f <- c(2, 3, 4)
at0H1 <- data.frame(a, b)

g <- c(1, 2, 3)
h <- c(2, 3, 4)
at0H1_1 <- data.frame(c, d)

# create lists of names
names <- list("at0H0", "at0H1")
namesLPC <- list("at0H0_1", "at0H1_1")

# column bind the data frames?
dfList <- list(cbind(names, namesLPC))
do.call(cbind, dfList)


But now I need it to create data frames for each. This do.call function just creates a list of the names of the data frames. Thanks!



(Edited to make reproducible code)










share|improve this question




















  • 1





    The example code is not reproducible and ls is a bad choice for a variable name because of the ls() function. - Try to clarify your questions.

    – Florian
    Nov 20 '18 at 0:19






  • 1





    do.call(cbind, mget(unlist(names))) for instance should do it with your full example.

    – thelatemail
    Nov 20 '18 at 0:47











  • thanks @thelatemail, but I need them to be separate data frames. For example, at0H0 and at0H0_1 need to be in one data frame and at0H1 and at0H1_1 need to be in a separate data frame

    – Lisa
    Nov 20 '18 at 0:57














1












1








1








I want to combine multiple sets of two data frames (a & a_1, b & b_1, etc.). Basically, I want to do what this question is asking. I created a list of my two data sets:



# create data 
a <- c(1, 2, 3)
b <- c(2, 3, 4)
at0H0 <- data.frame(a, b)

c <- c(1, 2, 3)
d <- c(2, 3, 4)
at0H0_1 <- data.frame(c, d)

e <- c(1, 2, 3)
f <- c(2, 3, 4)
at0H1 <- data.frame(a, b)

g <- c(1, 2, 3)
h <- c(2, 3, 4)
at0H1_1 <- data.frame(c, d)

# create lists of names
names <- list("at0H0", "at0H1")
namesLPC <- list("at0H0_1", "at0H1_1")

# column bind the data frames?
dfList <- list(cbind(names, namesLPC))
do.call(cbind, dfList)


But now I need it to create data frames for each. This do.call function just creates a list of the names of the data frames. Thanks!



(Edited to make reproducible code)










share|improve this question
















I want to combine multiple sets of two data frames (a & a_1, b & b_1, etc.). Basically, I want to do what this question is asking. I created a list of my two data sets:



# create data 
a <- c(1, 2, 3)
b <- c(2, 3, 4)
at0H0 <- data.frame(a, b)

c <- c(1, 2, 3)
d <- c(2, 3, 4)
at0H0_1 <- data.frame(c, d)

e <- c(1, 2, 3)
f <- c(2, 3, 4)
at0H1 <- data.frame(a, b)

g <- c(1, 2, 3)
h <- c(2, 3, 4)
at0H1_1 <- data.frame(c, d)

# create lists of names
names <- list("at0H0", "at0H1")
namesLPC <- list("at0H0_1", "at0H1_1")

# column bind the data frames?
dfList <- list(cbind(names, namesLPC))
do.call(cbind, dfList)


But now I need it to create data frames for each. This do.call function just creates a list of the names of the data frames. Thanks!



(Edited to make reproducible code)







r cbind






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 0:37







Lisa

















asked Nov 20 '18 at 0:12









LisaLisa

3741218




3741218








  • 1





    The example code is not reproducible and ls is a bad choice for a variable name because of the ls() function. - Try to clarify your questions.

    – Florian
    Nov 20 '18 at 0:19






  • 1





    do.call(cbind, mget(unlist(names))) for instance should do it with your full example.

    – thelatemail
    Nov 20 '18 at 0:47











  • thanks @thelatemail, but I need them to be separate data frames. For example, at0H0 and at0H0_1 need to be in one data frame and at0H1 and at0H1_1 need to be in a separate data frame

    – Lisa
    Nov 20 '18 at 0:57














  • 1





    The example code is not reproducible and ls is a bad choice for a variable name because of the ls() function. - Try to clarify your questions.

    – Florian
    Nov 20 '18 at 0:19






  • 1





    do.call(cbind, mget(unlist(names))) for instance should do it with your full example.

    – thelatemail
    Nov 20 '18 at 0:47











  • thanks @thelatemail, but I need them to be separate data frames. For example, at0H0 and at0H0_1 need to be in one data frame and at0H1 and at0H1_1 need to be in a separate data frame

    – Lisa
    Nov 20 '18 at 0:57








1




1





The example code is not reproducible and ls is a bad choice for a variable name because of the ls() function. - Try to clarify your questions.

– Florian
Nov 20 '18 at 0:19





The example code is not reproducible and ls is a bad choice for a variable name because of the ls() function. - Try to clarify your questions.

– Florian
Nov 20 '18 at 0:19




1




1





do.call(cbind, mget(unlist(names))) for instance should do it with your full example.

– thelatemail
Nov 20 '18 at 0:47





do.call(cbind, mget(unlist(names))) for instance should do it with your full example.

– thelatemail
Nov 20 '18 at 0:47













thanks @thelatemail, but I need them to be separate data frames. For example, at0H0 and at0H0_1 need to be in one data frame and at0H1 and at0H1_1 need to be in a separate data frame

– Lisa
Nov 20 '18 at 0:57





thanks @thelatemail, but I need them to be separate data frames. For example, at0H0 and at0H0_1 need to be in one data frame and at0H1 and at0H1_1 need to be in a separate data frame

– Lisa
Nov 20 '18 at 0:57












2 Answers
2






active

oldest

votes


















2














It's not super straight-forward, but with a little editing to a joining function you can get there:



joinfun <- function(x) do.call(cbind, unname(mget(x,inherits=TRUE)))
lapply(Map(c, names, namesLPC), joinfun)
#[[1]]
# a b c d
#1 1 2 1 2
#2 2 3 2 3
#3 3 4 3 4
#
#[[2]]
# a b c d
#1 1 2 1 2
#2 2 3 2 3
#3 3 4 3 4


The Map function pairs up the dataset names as required:



Map(c, names, namesLPC)
#[[1]]
#[1] "at0H0" "at0H0_1"
#
#[[2]]
#[1] "at0H1" "at0H1_1"


The lapply then loops over each part of the above list to mget (multiple-get) each object into a combined list. Like so, for the first part:



unname(mget(c("at0H0","at0H0_1"),inherits=TRUE))
#[[1]]
# a b
#1 1 2
#2 2 3
#3 3 4
#
#[[2]]
# c d
#1 1 2
#2 2 3
#3 3 4


Finally, do.call(cbind, ...) puts this combined list back into a single data.frame:



do.call(cbind, unname(mget(c("at0H0","at0H0_1"),inherits=TRUE)))
# a b c d
#1 1 2 1 2
#2 2 3 2 3
#3 3 4 3 4





share|improve this answer
























  • Thanks so much for your thorough explanation. I'm still having trouble getting the code to assign the sets of data to separate data frames. It has be to in separate data frames because there are different numbers of rows. For example, "at0H0" and "at0H0_1" have 839 rows, but "at0H1" and "at0H1_1" have 583 rows.

    – Lisa
    Nov 20 '18 at 14:18



















0














I've figured out a way to do it. A few notes: I have 360 data sets that I need to combine, which is why it is i in 1:360. This also names the data sets from an array of the names of the data sets (which is dataNames)



for (i in 1:360){
assign(paste(dataNames[i], sep = ""), cbind(names[[i]], namesLPC[[i]]))
}





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%2f53384438%2fassign-the-results-of-do-call-using-cbind-to-data-frames%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









    2














    It's not super straight-forward, but with a little editing to a joining function you can get there:



    joinfun <- function(x) do.call(cbind, unname(mget(x,inherits=TRUE)))
    lapply(Map(c, names, namesLPC), joinfun)
    #[[1]]
    # a b c d
    #1 1 2 1 2
    #2 2 3 2 3
    #3 3 4 3 4
    #
    #[[2]]
    # a b c d
    #1 1 2 1 2
    #2 2 3 2 3
    #3 3 4 3 4


    The Map function pairs up the dataset names as required:



    Map(c, names, namesLPC)
    #[[1]]
    #[1] "at0H0" "at0H0_1"
    #
    #[[2]]
    #[1] "at0H1" "at0H1_1"


    The lapply then loops over each part of the above list to mget (multiple-get) each object into a combined list. Like so, for the first part:



    unname(mget(c("at0H0","at0H0_1"),inherits=TRUE))
    #[[1]]
    # a b
    #1 1 2
    #2 2 3
    #3 3 4
    #
    #[[2]]
    # c d
    #1 1 2
    #2 2 3
    #3 3 4


    Finally, do.call(cbind, ...) puts this combined list back into a single data.frame:



    do.call(cbind, unname(mget(c("at0H0","at0H0_1"),inherits=TRUE)))
    # a b c d
    #1 1 2 1 2
    #2 2 3 2 3
    #3 3 4 3 4





    share|improve this answer
























    • Thanks so much for your thorough explanation. I'm still having trouble getting the code to assign the sets of data to separate data frames. It has be to in separate data frames because there are different numbers of rows. For example, "at0H0" and "at0H0_1" have 839 rows, but "at0H1" and "at0H1_1" have 583 rows.

      – Lisa
      Nov 20 '18 at 14:18
















    2














    It's not super straight-forward, but with a little editing to a joining function you can get there:



    joinfun <- function(x) do.call(cbind, unname(mget(x,inherits=TRUE)))
    lapply(Map(c, names, namesLPC), joinfun)
    #[[1]]
    # a b c d
    #1 1 2 1 2
    #2 2 3 2 3
    #3 3 4 3 4
    #
    #[[2]]
    # a b c d
    #1 1 2 1 2
    #2 2 3 2 3
    #3 3 4 3 4


    The Map function pairs up the dataset names as required:



    Map(c, names, namesLPC)
    #[[1]]
    #[1] "at0H0" "at0H0_1"
    #
    #[[2]]
    #[1] "at0H1" "at0H1_1"


    The lapply then loops over each part of the above list to mget (multiple-get) each object into a combined list. Like so, for the first part:



    unname(mget(c("at0H0","at0H0_1"),inherits=TRUE))
    #[[1]]
    # a b
    #1 1 2
    #2 2 3
    #3 3 4
    #
    #[[2]]
    # c d
    #1 1 2
    #2 2 3
    #3 3 4


    Finally, do.call(cbind, ...) puts this combined list back into a single data.frame:



    do.call(cbind, unname(mget(c("at0H0","at0H0_1"),inherits=TRUE)))
    # a b c d
    #1 1 2 1 2
    #2 2 3 2 3
    #3 3 4 3 4





    share|improve this answer
























    • Thanks so much for your thorough explanation. I'm still having trouble getting the code to assign the sets of data to separate data frames. It has be to in separate data frames because there are different numbers of rows. For example, "at0H0" and "at0H0_1" have 839 rows, but "at0H1" and "at0H1_1" have 583 rows.

      – Lisa
      Nov 20 '18 at 14:18














    2












    2








    2







    It's not super straight-forward, but with a little editing to a joining function you can get there:



    joinfun <- function(x) do.call(cbind, unname(mget(x,inherits=TRUE)))
    lapply(Map(c, names, namesLPC), joinfun)
    #[[1]]
    # a b c d
    #1 1 2 1 2
    #2 2 3 2 3
    #3 3 4 3 4
    #
    #[[2]]
    # a b c d
    #1 1 2 1 2
    #2 2 3 2 3
    #3 3 4 3 4


    The Map function pairs up the dataset names as required:



    Map(c, names, namesLPC)
    #[[1]]
    #[1] "at0H0" "at0H0_1"
    #
    #[[2]]
    #[1] "at0H1" "at0H1_1"


    The lapply then loops over each part of the above list to mget (multiple-get) each object into a combined list. Like so, for the first part:



    unname(mget(c("at0H0","at0H0_1"),inherits=TRUE))
    #[[1]]
    # a b
    #1 1 2
    #2 2 3
    #3 3 4
    #
    #[[2]]
    # c d
    #1 1 2
    #2 2 3
    #3 3 4


    Finally, do.call(cbind, ...) puts this combined list back into a single data.frame:



    do.call(cbind, unname(mget(c("at0H0","at0H0_1"),inherits=TRUE)))
    # a b c d
    #1 1 2 1 2
    #2 2 3 2 3
    #3 3 4 3 4





    share|improve this answer













    It's not super straight-forward, but with a little editing to a joining function you can get there:



    joinfun <- function(x) do.call(cbind, unname(mget(x,inherits=TRUE)))
    lapply(Map(c, names, namesLPC), joinfun)
    #[[1]]
    # a b c d
    #1 1 2 1 2
    #2 2 3 2 3
    #3 3 4 3 4
    #
    #[[2]]
    # a b c d
    #1 1 2 1 2
    #2 2 3 2 3
    #3 3 4 3 4


    The Map function pairs up the dataset names as required:



    Map(c, names, namesLPC)
    #[[1]]
    #[1] "at0H0" "at0H0_1"
    #
    #[[2]]
    #[1] "at0H1" "at0H1_1"


    The lapply then loops over each part of the above list to mget (multiple-get) each object into a combined list. Like so, for the first part:



    unname(mget(c("at0H0","at0H0_1"),inherits=TRUE))
    #[[1]]
    # a b
    #1 1 2
    #2 2 3
    #3 3 4
    #
    #[[2]]
    # c d
    #1 1 2
    #2 2 3
    #3 3 4


    Finally, do.call(cbind, ...) puts this combined list back into a single data.frame:



    do.call(cbind, unname(mget(c("at0H0","at0H0_1"),inherits=TRUE)))
    # a b c d
    #1 1 2 1 2
    #2 2 3 2 3
    #3 3 4 3 4






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 1:44









    thelatemailthelatemail

    67.1k881149




    67.1k881149













    • Thanks so much for your thorough explanation. I'm still having trouble getting the code to assign the sets of data to separate data frames. It has be to in separate data frames because there are different numbers of rows. For example, "at0H0" and "at0H0_1" have 839 rows, but "at0H1" and "at0H1_1" have 583 rows.

      – Lisa
      Nov 20 '18 at 14:18



















    • Thanks so much for your thorough explanation. I'm still having trouble getting the code to assign the sets of data to separate data frames. It has be to in separate data frames because there are different numbers of rows. For example, "at0H0" and "at0H0_1" have 839 rows, but "at0H1" and "at0H1_1" have 583 rows.

      – Lisa
      Nov 20 '18 at 14:18

















    Thanks so much for your thorough explanation. I'm still having trouble getting the code to assign the sets of data to separate data frames. It has be to in separate data frames because there are different numbers of rows. For example, "at0H0" and "at0H0_1" have 839 rows, but "at0H1" and "at0H1_1" have 583 rows.

    – Lisa
    Nov 20 '18 at 14:18





    Thanks so much for your thorough explanation. I'm still having trouble getting the code to assign the sets of data to separate data frames. It has be to in separate data frames because there are different numbers of rows. For example, "at0H0" and "at0H0_1" have 839 rows, but "at0H1" and "at0H1_1" have 583 rows.

    – Lisa
    Nov 20 '18 at 14:18













    0














    I've figured out a way to do it. A few notes: I have 360 data sets that I need to combine, which is why it is i in 1:360. This also names the data sets from an array of the names of the data sets (which is dataNames)



    for (i in 1:360){
    assign(paste(dataNames[i], sep = ""), cbind(names[[i]], namesLPC[[i]]))
    }





    share|improve this answer




























      0














      I've figured out a way to do it. A few notes: I have 360 data sets that I need to combine, which is why it is i in 1:360. This also names the data sets from an array of the names of the data sets (which is dataNames)



      for (i in 1:360){
      assign(paste(dataNames[i], sep = ""), cbind(names[[i]], namesLPC[[i]]))
      }





      share|improve this answer


























        0












        0








        0







        I've figured out a way to do it. A few notes: I have 360 data sets that I need to combine, which is why it is i in 1:360. This also names the data sets from an array of the names of the data sets (which is dataNames)



        for (i in 1:360){
        assign(paste(dataNames[i], sep = ""), cbind(names[[i]], namesLPC[[i]]))
        }





        share|improve this answer













        I've figured out a way to do it. A few notes: I have 360 data sets that I need to combine, which is why it is i in 1:360. This also names the data sets from an array of the names of the data sets (which is dataNames)



        for (i in 1:360){
        assign(paste(dataNames[i], sep = ""), cbind(names[[i]], namesLPC[[i]]))
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 14:52









        LisaLisa

        3741218




        3741218






























            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%2f53384438%2fassign-the-results-of-do-call-using-cbind-to-data-frames%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

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            Npm cannot find a required file even through it is in the searched directory