How to split a data frame into multiple dataframes with each two columns as a new dataframe?

后端 未结 2 866
滥情空心
滥情空心 2020-12-06 23:06

I\'ve the below dataframe with 10 columns.

V1  V2  V3  V4  V5  V6  V7  V8  V9  V10
1   2   3   4   5   6   7   8   9   10
11  12  13  14  15  16  17  18  19         


        
相关标签:
2条回答
  • 2020-12-06 23:20

    We can use seq to create the index of alternating columns, loop along and then subset the dataset

    lst1 <- lapply(seq(1, ncol(df1), by=2), function(i) 
                       df1[i: pmin((i+1), ncol(df1))])
    

    Or use split

     lst2 <- lapply(split(seq_along(df1),as.numeric(gl(ncol(df1), 
               2, ncol(df1)))), function(i) df1[i])
    

    If we need 5 individual datasets in the global environment, use list2env (not recommended though)

    list2env(setNames(lst1, paste0("newdf", seq_along(lst1))),
               envir=.GlobalEnv)
    

    data

    df1 <- structure(list(V1 = c(1L, 11L, 21L, 31L, 41L, 51L, 61L, 71L, 
    81L, 91L), V2 = c(2L, 12L, 22L, 32L, 42L, 52L, 62L, 72L, 82L, 
    92L), V3 = c(3L, 13L, 23L, 33L, 43L, 53L, 63L, 73L, 83L, 93L), 
        V4 = c(4L, 14L, 24L, 34L, 44L, 54L, 64L, 74L, 84L, 94L), 
        V5 = c(5L, 15L, 25L, 35L, 45L, 55L, 65L, 75L, 85L, 95L), 
        V6 = c(6L, 16L, 26L, 36L, 46L, 56L, 66L, 76L, 86L, 96L), 
        V7 = c(7L, 17L, 27L, 37L, 47L, 57L, 67L, 77L, 87L, 97L), 
        V8 = c(8L, 18L, 28L, 38L, 48L, 58L, 68L, 78L, 88L, 98L), 
        V9 = c(9L, 19L, 29L, 39L, 49L, 59L, 69L, 79L, 89L, 99L), 
        V10 = c(10L, 20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L, 100L
        )), .Names = c("V1", "V2", "V3", "V4", "V5", "V6", "V7", 
    "V8", "V9", "V10"), class = "data.frame", row.names = c(NA, -10L
    ))
    
    0 讨论(0)
  • 2020-12-06 23:34

    Try tapply with an INDEX argument of 1, 1, 2, 2, etc.

    tapply(as.list(DF), gl(ncol(DF)/2, 2), as.data.frame)
    

    giving (continued below output):

    $`1`
       V1 V2
    1   1  2
    2  11 12
    3  21 22
    4  31 32
    5  41 42
    6  51 52
    7  61 62
    8  71 72
    9  81 82
    10 91 92
    
    $`2`
       V3 V4
    1   3  4
    2  13 14
    3  23 24
    4  33 34
    5  43 44
    6  53 54
    7  63 64
    8  73 74
    9  83 84
    10 93 94
    
    $`3`
       V5 V6
    1   5  6
    2  15 16
    3  25 26
    4  35 36
    5  45 46
    6  55 56
    7  65 66
    8  75 76
    9  85 86
    10 95 96
    
    $`4`
       V7 V8
    1   7  8
    2  17 18
    3  27 28
    4  37 38
    5  47 48
    6  57 58
    7  67 68
    8  77 78
    9  87 88
    10 97 98
    

    Another possibility if there is an all numeric data frame as in the question is to reshape it into an array:

    a <- array(unlist(DF), c(nrow(DF), 2, ncol(DF)/2))
    

    in which case a[,,i] is the ith matrix for i = 1, ..., ncol(DF)/2 .

    Note: The input DF in reproducible form is:

    DF <- as.data.frame(matrix(1:100, 10, byrow = TRUE))
    
    0 讨论(0)
提交回复
热议问题