Nesting several groups of columns inside a data frame





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







4















The concept of nesting several columns into a single list-column is very powerful. However, I am not sure whether it is possible at all to nest more than one set of columns into several list-columns within the same pipeline using the nest function in {tidyr}. For instance, assume I have the following data frame:



df <- as.data.frame(replicate(6, runif(10) * 100))

colnames(df) <- c(
paste0("a", 1:2), # a1, a2
paste0("b", 1:4) # b1, b2, b3, b4
)

df
a1 a2 b1 b2 b3 b4
1 20.807348 69.339482 91.837151 99.76813 3.394350 33.780049
2 64.667733 20.676381 80.523369 38.42774 85.635208 60.111491
3 55.352501 55.699571 4.812923 38.65333 98.869203 80.345576
4 45.194094 16.511696 83.834651 51.48698 7.191081 16.697210
5 66.401642 89.041055 26.965636 67.90061 90.622428 59.552935
6 35.750100 55.997766 49.768556 68.45900 67.523080 58.993232
7 21.392823 5.335281 56.348328 35.68331 51.029617 66.290035
8 8.851236 19.486580 14.199370 22.49754 14.617592 18.236406
9 70.475652 6.229997 43.169364 12.63378 21.415589 2.163004
10 47.837613 37.641530 38.001288 71.15896 71.000568 2.135611


I would like to nest the "a" columns into a list-column AND nest the "b" columns into a second list-column because I would like to perform different computations on them.



Nesting the "a" columns works:



library(tidyr)
nest(df, a1, a2, .key = "a")

b1 b2 b3 b4 a
1 91.837151 99.76813 3.394350 33.780049 20.80735, 69.33948
2 80.523369 38.42774 85.635208 60.111491 64.66773, 20.67638
3 4.812923 38.65333 98.869203 80.345576 55.35250, 55.69957
4 83.834651 51.48698 7.191081 16.697210 45.19409, 16.51170
5 26.965636 67.90061 90.622428 59.552935 66.40164, 89.04105
6 49.768556 68.45900 67.523080 58.993232 35.75010, 55.99777
7 56.348328 35.68331 51.029617 66.290035 21.392823, 5.335281
8 14.199370 22.49754 14.617592 18.236406 8.851236, 19.486580
9 43.169364 12.63378 21.415589 2.163004 70.475652, 6.229997
10 38.001288 71.15896 71.000568 2.135611 47.83761, 37.64153


But it is impossible to nest the "b" columns AFTER the "a" columns have been nested:



nest(df, a1, a2, .key = "a") %>%
nest(b1, b2, b3, b4, .key = "b")
Error in grouped_df_impl(data, unname(vars), drop) :
Column `a` can't be used as a grouping variable because it's a list


which makes sense by reading the error message.



My work-around is to:




  • nest the "a" columns


  • perform the required computations on the "a" list-column


  • unnest the "a" list-column


  • nest the "b" columns


  • perform the required computations on the "b" list-column


  • unnest the "b" list-column



Is there a more straight-forward way to achieve this? Your help is much appreciated.










share|improve this question























  • Would you want to gather the data into a long format, so you have an "a" column and a "b" column, and nest by groups that way? I'm having a little trouble picturing what you're after

    – camille
    Nov 16 '18 at 19:28











  • @camille Yes, your solution would work normally, but not just in my case. "a" and "b" are "tests" and "homework" and my goal is to drop the lowest score of each evaluation type. So I want to first nest them into 1-row data frames and sort them this way before calculating the final grade.

    – SavedByJESUS
    Nov 16 '18 at 19:33


















4















The concept of nesting several columns into a single list-column is very powerful. However, I am not sure whether it is possible at all to nest more than one set of columns into several list-columns within the same pipeline using the nest function in {tidyr}. For instance, assume I have the following data frame:



df <- as.data.frame(replicate(6, runif(10) * 100))

colnames(df) <- c(
paste0("a", 1:2), # a1, a2
paste0("b", 1:4) # b1, b2, b3, b4
)

df
a1 a2 b1 b2 b3 b4
1 20.807348 69.339482 91.837151 99.76813 3.394350 33.780049
2 64.667733 20.676381 80.523369 38.42774 85.635208 60.111491
3 55.352501 55.699571 4.812923 38.65333 98.869203 80.345576
4 45.194094 16.511696 83.834651 51.48698 7.191081 16.697210
5 66.401642 89.041055 26.965636 67.90061 90.622428 59.552935
6 35.750100 55.997766 49.768556 68.45900 67.523080 58.993232
7 21.392823 5.335281 56.348328 35.68331 51.029617 66.290035
8 8.851236 19.486580 14.199370 22.49754 14.617592 18.236406
9 70.475652 6.229997 43.169364 12.63378 21.415589 2.163004
10 47.837613 37.641530 38.001288 71.15896 71.000568 2.135611


I would like to nest the "a" columns into a list-column AND nest the "b" columns into a second list-column because I would like to perform different computations on them.



Nesting the "a" columns works:



library(tidyr)
nest(df, a1, a2, .key = "a")

b1 b2 b3 b4 a
1 91.837151 99.76813 3.394350 33.780049 20.80735, 69.33948
2 80.523369 38.42774 85.635208 60.111491 64.66773, 20.67638
3 4.812923 38.65333 98.869203 80.345576 55.35250, 55.69957
4 83.834651 51.48698 7.191081 16.697210 45.19409, 16.51170
5 26.965636 67.90061 90.622428 59.552935 66.40164, 89.04105
6 49.768556 68.45900 67.523080 58.993232 35.75010, 55.99777
7 56.348328 35.68331 51.029617 66.290035 21.392823, 5.335281
8 14.199370 22.49754 14.617592 18.236406 8.851236, 19.486580
9 43.169364 12.63378 21.415589 2.163004 70.475652, 6.229997
10 38.001288 71.15896 71.000568 2.135611 47.83761, 37.64153


But it is impossible to nest the "b" columns AFTER the "a" columns have been nested:



nest(df, a1, a2, .key = "a") %>%
nest(b1, b2, b3, b4, .key = "b")
Error in grouped_df_impl(data, unname(vars), drop) :
Column `a` can't be used as a grouping variable because it's a list


which makes sense by reading the error message.



My work-around is to:




  • nest the "a" columns


  • perform the required computations on the "a" list-column


  • unnest the "a" list-column


  • nest the "b" columns


  • perform the required computations on the "b" list-column


  • unnest the "b" list-column



Is there a more straight-forward way to achieve this? Your help is much appreciated.










share|improve this question























  • Would you want to gather the data into a long format, so you have an "a" column and a "b" column, and nest by groups that way? I'm having a little trouble picturing what you're after

    – camille
    Nov 16 '18 at 19:28











  • @camille Yes, your solution would work normally, but not just in my case. "a" and "b" are "tests" and "homework" and my goal is to drop the lowest score of each evaluation type. So I want to first nest them into 1-row data frames and sort them this way before calculating the final grade.

    – SavedByJESUS
    Nov 16 '18 at 19:33














4












4








4


1






The concept of nesting several columns into a single list-column is very powerful. However, I am not sure whether it is possible at all to nest more than one set of columns into several list-columns within the same pipeline using the nest function in {tidyr}. For instance, assume I have the following data frame:



df <- as.data.frame(replicate(6, runif(10) * 100))

colnames(df) <- c(
paste0("a", 1:2), # a1, a2
paste0("b", 1:4) # b1, b2, b3, b4
)

df
a1 a2 b1 b2 b3 b4
1 20.807348 69.339482 91.837151 99.76813 3.394350 33.780049
2 64.667733 20.676381 80.523369 38.42774 85.635208 60.111491
3 55.352501 55.699571 4.812923 38.65333 98.869203 80.345576
4 45.194094 16.511696 83.834651 51.48698 7.191081 16.697210
5 66.401642 89.041055 26.965636 67.90061 90.622428 59.552935
6 35.750100 55.997766 49.768556 68.45900 67.523080 58.993232
7 21.392823 5.335281 56.348328 35.68331 51.029617 66.290035
8 8.851236 19.486580 14.199370 22.49754 14.617592 18.236406
9 70.475652 6.229997 43.169364 12.63378 21.415589 2.163004
10 47.837613 37.641530 38.001288 71.15896 71.000568 2.135611


I would like to nest the "a" columns into a list-column AND nest the "b" columns into a second list-column because I would like to perform different computations on them.



Nesting the "a" columns works:



library(tidyr)
nest(df, a1, a2, .key = "a")

b1 b2 b3 b4 a
1 91.837151 99.76813 3.394350 33.780049 20.80735, 69.33948
2 80.523369 38.42774 85.635208 60.111491 64.66773, 20.67638
3 4.812923 38.65333 98.869203 80.345576 55.35250, 55.69957
4 83.834651 51.48698 7.191081 16.697210 45.19409, 16.51170
5 26.965636 67.90061 90.622428 59.552935 66.40164, 89.04105
6 49.768556 68.45900 67.523080 58.993232 35.75010, 55.99777
7 56.348328 35.68331 51.029617 66.290035 21.392823, 5.335281
8 14.199370 22.49754 14.617592 18.236406 8.851236, 19.486580
9 43.169364 12.63378 21.415589 2.163004 70.475652, 6.229997
10 38.001288 71.15896 71.000568 2.135611 47.83761, 37.64153


But it is impossible to nest the "b" columns AFTER the "a" columns have been nested:



nest(df, a1, a2, .key = "a") %>%
nest(b1, b2, b3, b4, .key = "b")
Error in grouped_df_impl(data, unname(vars), drop) :
Column `a` can't be used as a grouping variable because it's a list


which makes sense by reading the error message.



My work-around is to:




  • nest the "a" columns


  • perform the required computations on the "a" list-column


  • unnest the "a" list-column


  • nest the "b" columns


  • perform the required computations on the "b" list-column


  • unnest the "b" list-column



Is there a more straight-forward way to achieve this? Your help is much appreciated.










share|improve this question














The concept of nesting several columns into a single list-column is very powerful. However, I am not sure whether it is possible at all to nest more than one set of columns into several list-columns within the same pipeline using the nest function in {tidyr}. For instance, assume I have the following data frame:



df <- as.data.frame(replicate(6, runif(10) * 100))

colnames(df) <- c(
paste0("a", 1:2), # a1, a2
paste0("b", 1:4) # b1, b2, b3, b4
)

df
a1 a2 b1 b2 b3 b4
1 20.807348 69.339482 91.837151 99.76813 3.394350 33.780049
2 64.667733 20.676381 80.523369 38.42774 85.635208 60.111491
3 55.352501 55.699571 4.812923 38.65333 98.869203 80.345576
4 45.194094 16.511696 83.834651 51.48698 7.191081 16.697210
5 66.401642 89.041055 26.965636 67.90061 90.622428 59.552935
6 35.750100 55.997766 49.768556 68.45900 67.523080 58.993232
7 21.392823 5.335281 56.348328 35.68331 51.029617 66.290035
8 8.851236 19.486580 14.199370 22.49754 14.617592 18.236406
9 70.475652 6.229997 43.169364 12.63378 21.415589 2.163004
10 47.837613 37.641530 38.001288 71.15896 71.000568 2.135611


I would like to nest the "a" columns into a list-column AND nest the "b" columns into a second list-column because I would like to perform different computations on them.



Nesting the "a" columns works:



library(tidyr)
nest(df, a1, a2, .key = "a")

b1 b2 b3 b4 a
1 91.837151 99.76813 3.394350 33.780049 20.80735, 69.33948
2 80.523369 38.42774 85.635208 60.111491 64.66773, 20.67638
3 4.812923 38.65333 98.869203 80.345576 55.35250, 55.69957
4 83.834651 51.48698 7.191081 16.697210 45.19409, 16.51170
5 26.965636 67.90061 90.622428 59.552935 66.40164, 89.04105
6 49.768556 68.45900 67.523080 58.993232 35.75010, 55.99777
7 56.348328 35.68331 51.029617 66.290035 21.392823, 5.335281
8 14.199370 22.49754 14.617592 18.236406 8.851236, 19.486580
9 43.169364 12.63378 21.415589 2.163004 70.475652, 6.229997
10 38.001288 71.15896 71.000568 2.135611 47.83761, 37.64153


But it is impossible to nest the "b" columns AFTER the "a" columns have been nested:



nest(df, a1, a2, .key = "a") %>%
nest(b1, b2, b3, b4, .key = "b")
Error in grouped_df_impl(data, unname(vars), drop) :
Column `a` can't be used as a grouping variable because it's a list


which makes sense by reading the error message.



My work-around is to:




  • nest the "a" columns


  • perform the required computations on the "a" list-column


  • unnest the "a" list-column


  • nest the "b" columns


  • perform the required computations on the "b" list-column


  • unnest the "b" list-column



Is there a more straight-forward way to achieve this? Your help is much appreciated.







r tidyverse tidyr






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 19:15









SavedByJESUSSavedByJESUS

99431430




99431430













  • Would you want to gather the data into a long format, so you have an "a" column and a "b" column, and nest by groups that way? I'm having a little trouble picturing what you're after

    – camille
    Nov 16 '18 at 19:28











  • @camille Yes, your solution would work normally, but not just in my case. "a" and "b" are "tests" and "homework" and my goal is to drop the lowest score of each evaluation type. So I want to first nest them into 1-row data frames and sort them this way before calculating the final grade.

    – SavedByJESUS
    Nov 16 '18 at 19:33



















  • Would you want to gather the data into a long format, so you have an "a" column and a "b" column, and nest by groups that way? I'm having a little trouble picturing what you're after

    – camille
    Nov 16 '18 at 19:28











  • @camille Yes, your solution would work normally, but not just in my case. "a" and "b" are "tests" and "homework" and my goal is to drop the lowest score of each evaluation type. So I want to first nest them into 1-row data frames and sort them this way before calculating the final grade.

    – SavedByJESUS
    Nov 16 '18 at 19:33

















Would you want to gather the data into a long format, so you have an "a" column and a "b" column, and nest by groups that way? I'm having a little trouble picturing what you're after

– camille
Nov 16 '18 at 19:28





Would you want to gather the data into a long format, so you have an "a" column and a "b" column, and nest by groups that way? I'm having a little trouble picturing what you're after

– camille
Nov 16 '18 at 19:28













@camille Yes, your solution would work normally, but not just in my case. "a" and "b" are "tests" and "homework" and my goal is to drop the lowest score of each evaluation type. So I want to first nest them into 1-row data frames and sort them this way before calculating the final grade.

– SavedByJESUS
Nov 16 '18 at 19:33





@camille Yes, your solution would work normally, but not just in my case. "a" and "b" are "tests" and "homework" and my goal is to drop the lowest score of each evaluation type. So I want to first nest them into 1-row data frames and sort them this way before calculating the final grade.

– SavedByJESUS
Nov 16 '18 at 19:33












1 Answer
1






active

oldest

votes


















4














We can use map to do this



library(tidyverse)
out <- list('a', 'b') %>%
map(~ df %>%
select(matches(.x)) %>%
nest(names(.), .key = !! rlang::sym(.x))) %>%
bind_cols
out
# A tibble: 1 x 2
# a b
# <list> <list>
#1 <data.frame [10 × 2]> <data.frame [10 × 4]>


out %>%
unnest
# A tibble: 10 x 6
# a1 a2 b1 b2 b3 b4
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 20.8 69.3 91.8 99.8 3.39 33.8
# 2 64.7 20.7 80.5 38.4 85.6 60.1
# 3 55.4 55.7 4.81 38.7 98.9 80.3
# 4 45.2 16.5 83.8 51.5 7.19 16.7
# 5 66.4 89.0 27.0 67.9 90.6 59.6
# 6 35.8 56.0 49.8 68.5 67.5 59.0
# 7 21.4 5.34 56.3 35.7 51.0 66.3
# 8 8.85 19.5 14.2 22.5 14.6 18.2
# 9 70.5 6.23 43.2 12.6 21.4 2.16
#10 47.8 37.6 38.0 71.2 71.0 2.14


We could do the separate computations on the 'a' and 'b' list of columns



out %>%
mutate(a = map(a, `*`, 4)) %>%
unnest
# A tibble: 10 x 6
# a1 a2 b1 b2 b3 b4
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 83.2 277. 91.8 99.8 3.39 33.8
# 2 259. 82.7 80.5 38.4 85.6 60.1
# 3 221. 223. 4.81 38.7 98.9 80.3
# 4 181. 66.0 83.8 51.5 7.19 16.7
# 5 266. 356. 27.0 67.9 90.6 59.6
# 6 143. 224. 49.8 68.5 67.5 59.0
# 7 85.6 21.3 56.3 35.7 51.0 66.3
# 8 35.4 77.9 14.2 22.5 14.6 18.2
# 9 282. 24.9 43.2 12.6 21.4 2.16
#10 191. 151. 38.0 71.2 71.0 2.14


Having said that, it is also possible to select columns of interest with mutate_at instead of doing nest/unnest



df %>% 
mutate_at(vars(matches('^a\d+')), funs(.*4))





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%2f53344067%2fnesting-several-groups-of-columns-inside-a-data-frame%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    We can use map to do this



    library(tidyverse)
    out <- list('a', 'b') %>%
    map(~ df %>%
    select(matches(.x)) %>%
    nest(names(.), .key = !! rlang::sym(.x))) %>%
    bind_cols
    out
    # A tibble: 1 x 2
    # a b
    # <list> <list>
    #1 <data.frame [10 × 2]> <data.frame [10 × 4]>


    out %>%
    unnest
    # A tibble: 10 x 6
    # a1 a2 b1 b2 b3 b4
    # <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
    # 1 20.8 69.3 91.8 99.8 3.39 33.8
    # 2 64.7 20.7 80.5 38.4 85.6 60.1
    # 3 55.4 55.7 4.81 38.7 98.9 80.3
    # 4 45.2 16.5 83.8 51.5 7.19 16.7
    # 5 66.4 89.0 27.0 67.9 90.6 59.6
    # 6 35.8 56.0 49.8 68.5 67.5 59.0
    # 7 21.4 5.34 56.3 35.7 51.0 66.3
    # 8 8.85 19.5 14.2 22.5 14.6 18.2
    # 9 70.5 6.23 43.2 12.6 21.4 2.16
    #10 47.8 37.6 38.0 71.2 71.0 2.14


    We could do the separate computations on the 'a' and 'b' list of columns



    out %>%
    mutate(a = map(a, `*`, 4)) %>%
    unnest
    # A tibble: 10 x 6
    # a1 a2 b1 b2 b3 b4
    # <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
    # 1 83.2 277. 91.8 99.8 3.39 33.8
    # 2 259. 82.7 80.5 38.4 85.6 60.1
    # 3 221. 223. 4.81 38.7 98.9 80.3
    # 4 181. 66.0 83.8 51.5 7.19 16.7
    # 5 266. 356. 27.0 67.9 90.6 59.6
    # 6 143. 224. 49.8 68.5 67.5 59.0
    # 7 85.6 21.3 56.3 35.7 51.0 66.3
    # 8 35.4 77.9 14.2 22.5 14.6 18.2
    # 9 282. 24.9 43.2 12.6 21.4 2.16
    #10 191. 151. 38.0 71.2 71.0 2.14


    Having said that, it is also possible to select columns of interest with mutate_at instead of doing nest/unnest



    df %>% 
    mutate_at(vars(matches('^a\d+')), funs(.*4))





    share|improve this answer






























      4














      We can use map to do this



      library(tidyverse)
      out <- list('a', 'b') %>%
      map(~ df %>%
      select(matches(.x)) %>%
      nest(names(.), .key = !! rlang::sym(.x))) %>%
      bind_cols
      out
      # A tibble: 1 x 2
      # a b
      # <list> <list>
      #1 <data.frame [10 × 2]> <data.frame [10 × 4]>


      out %>%
      unnest
      # A tibble: 10 x 6
      # a1 a2 b1 b2 b3 b4
      # <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
      # 1 20.8 69.3 91.8 99.8 3.39 33.8
      # 2 64.7 20.7 80.5 38.4 85.6 60.1
      # 3 55.4 55.7 4.81 38.7 98.9 80.3
      # 4 45.2 16.5 83.8 51.5 7.19 16.7
      # 5 66.4 89.0 27.0 67.9 90.6 59.6
      # 6 35.8 56.0 49.8 68.5 67.5 59.0
      # 7 21.4 5.34 56.3 35.7 51.0 66.3
      # 8 8.85 19.5 14.2 22.5 14.6 18.2
      # 9 70.5 6.23 43.2 12.6 21.4 2.16
      #10 47.8 37.6 38.0 71.2 71.0 2.14


      We could do the separate computations on the 'a' and 'b' list of columns



      out %>%
      mutate(a = map(a, `*`, 4)) %>%
      unnest
      # A tibble: 10 x 6
      # a1 a2 b1 b2 b3 b4
      # <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
      # 1 83.2 277. 91.8 99.8 3.39 33.8
      # 2 259. 82.7 80.5 38.4 85.6 60.1
      # 3 221. 223. 4.81 38.7 98.9 80.3
      # 4 181. 66.0 83.8 51.5 7.19 16.7
      # 5 266. 356. 27.0 67.9 90.6 59.6
      # 6 143. 224. 49.8 68.5 67.5 59.0
      # 7 85.6 21.3 56.3 35.7 51.0 66.3
      # 8 35.4 77.9 14.2 22.5 14.6 18.2
      # 9 282. 24.9 43.2 12.6 21.4 2.16
      #10 191. 151. 38.0 71.2 71.0 2.14


      Having said that, it is also possible to select columns of interest with mutate_at instead of doing nest/unnest



      df %>% 
      mutate_at(vars(matches('^a\d+')), funs(.*4))





      share|improve this answer




























        4












        4








        4







        We can use map to do this



        library(tidyverse)
        out <- list('a', 'b') %>%
        map(~ df %>%
        select(matches(.x)) %>%
        nest(names(.), .key = !! rlang::sym(.x))) %>%
        bind_cols
        out
        # A tibble: 1 x 2
        # a b
        # <list> <list>
        #1 <data.frame [10 × 2]> <data.frame [10 × 4]>


        out %>%
        unnest
        # A tibble: 10 x 6
        # a1 a2 b1 b2 b3 b4
        # <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
        # 1 20.8 69.3 91.8 99.8 3.39 33.8
        # 2 64.7 20.7 80.5 38.4 85.6 60.1
        # 3 55.4 55.7 4.81 38.7 98.9 80.3
        # 4 45.2 16.5 83.8 51.5 7.19 16.7
        # 5 66.4 89.0 27.0 67.9 90.6 59.6
        # 6 35.8 56.0 49.8 68.5 67.5 59.0
        # 7 21.4 5.34 56.3 35.7 51.0 66.3
        # 8 8.85 19.5 14.2 22.5 14.6 18.2
        # 9 70.5 6.23 43.2 12.6 21.4 2.16
        #10 47.8 37.6 38.0 71.2 71.0 2.14


        We could do the separate computations on the 'a' and 'b' list of columns



        out %>%
        mutate(a = map(a, `*`, 4)) %>%
        unnest
        # A tibble: 10 x 6
        # a1 a2 b1 b2 b3 b4
        # <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
        # 1 83.2 277. 91.8 99.8 3.39 33.8
        # 2 259. 82.7 80.5 38.4 85.6 60.1
        # 3 221. 223. 4.81 38.7 98.9 80.3
        # 4 181. 66.0 83.8 51.5 7.19 16.7
        # 5 266. 356. 27.0 67.9 90.6 59.6
        # 6 143. 224. 49.8 68.5 67.5 59.0
        # 7 85.6 21.3 56.3 35.7 51.0 66.3
        # 8 35.4 77.9 14.2 22.5 14.6 18.2
        # 9 282. 24.9 43.2 12.6 21.4 2.16
        #10 191. 151. 38.0 71.2 71.0 2.14


        Having said that, it is also possible to select columns of interest with mutate_at instead of doing nest/unnest



        df %>% 
        mutate_at(vars(matches('^a\d+')), funs(.*4))





        share|improve this answer















        We can use map to do this



        library(tidyverse)
        out <- list('a', 'b') %>%
        map(~ df %>%
        select(matches(.x)) %>%
        nest(names(.), .key = !! rlang::sym(.x))) %>%
        bind_cols
        out
        # A tibble: 1 x 2
        # a b
        # <list> <list>
        #1 <data.frame [10 × 2]> <data.frame [10 × 4]>


        out %>%
        unnest
        # A tibble: 10 x 6
        # a1 a2 b1 b2 b3 b4
        # <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
        # 1 20.8 69.3 91.8 99.8 3.39 33.8
        # 2 64.7 20.7 80.5 38.4 85.6 60.1
        # 3 55.4 55.7 4.81 38.7 98.9 80.3
        # 4 45.2 16.5 83.8 51.5 7.19 16.7
        # 5 66.4 89.0 27.0 67.9 90.6 59.6
        # 6 35.8 56.0 49.8 68.5 67.5 59.0
        # 7 21.4 5.34 56.3 35.7 51.0 66.3
        # 8 8.85 19.5 14.2 22.5 14.6 18.2
        # 9 70.5 6.23 43.2 12.6 21.4 2.16
        #10 47.8 37.6 38.0 71.2 71.0 2.14


        We could do the separate computations on the 'a' and 'b' list of columns



        out %>%
        mutate(a = map(a, `*`, 4)) %>%
        unnest
        # A tibble: 10 x 6
        # a1 a2 b1 b2 b3 b4
        # <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
        # 1 83.2 277. 91.8 99.8 3.39 33.8
        # 2 259. 82.7 80.5 38.4 85.6 60.1
        # 3 221. 223. 4.81 38.7 98.9 80.3
        # 4 181. 66.0 83.8 51.5 7.19 16.7
        # 5 266. 356. 27.0 67.9 90.6 59.6
        # 6 143. 224. 49.8 68.5 67.5 59.0
        # 7 85.6 21.3 56.3 35.7 51.0 66.3
        # 8 35.4 77.9 14.2 22.5 14.6 18.2
        # 9 282. 24.9 43.2 12.6 21.4 2.16
        #10 191. 151. 38.0 71.2 71.0 2.14


        Having said that, it is also possible to select columns of interest with mutate_at instead of doing nest/unnest



        df %>% 
        mutate_at(vars(matches('^a\d+')), funs(.*4))






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 19:28

























        answered Nov 16 '18 at 19:21









        akrunakrun

        422k13209285




        422k13209285
































            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%2f53344067%2fnesting-several-groups-of-columns-inside-a-data-frame%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

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values