Home > other >  How to get every element from 1-15 into a list from a vector that has over 800 elements, in R
How to get every element from 1-15 into a list from a vector that has over 800 elements, in R

Time:08-12

I have a vector named all_teams.

   [1] "Arizona Cardinals"        "1920"                     "2022"                     "577"                     
   [5] "777"                      "41"                       ".426"                     "11"                      
   [9] "7"                        "10"                       "0.412"                    "2"                       
  [13] "0"                        "1"                        "8"                        "Chicago Cardinals"       
  [17] "1920"                     "1943"                     "99"                       "141"                     
  [21] "21"                       ".413"                     "0"                        "0"                       
  [25] "0"                        ""                         "1"                        "0"                       
  [29] "0"                        "1"                        "Chi/Pit Cards/Steelers"   "1944"                    
  [33] "1944"                     "0"                        "10"                       "0"                       
  [37] ".000"                     "0"                        "0"                        "0"                       
  [41] ""                         "0"                        "0"                        "0"                       
  [45] "0"                        "Chicago Cardinals"        "1945"                     "1959"                    
  [49] "66"                       "107"                      "4"                        ".382"                    
  [53] "2"                        "1"                        "1"                        ""                        
  [57] "1"                        "0"                        "0"                        "2"   

I am trying to figure out how to extract 1-15 elements into a list and my desired output is:

[1] "Arizona Cardinals"        "1920"                     "2022"                     "577"                     
    "777"                      "41"                       ".426"                     "11"                      
    "7"                        "10"                       "0.412"                    "2"                       
    "0"                        "1"                        "8"                        
[2] "Chicago Cardinals"        "1920"                     "1943"                     "99"                       
    "141"                      "21"                       ".413"                     "0"                        
    "0"                        "0"                        ""                         "1"                        
    "0"                        "0"                        "1"                        
[3] "Chi/Pit Cards/Steelers"   "1944"                     "1944"                     "0"                        
    "10"                       "0"                        ".000"                     "0"                        
    "0"                        "0"                        ""                         "0"                        
    "0"                        "0"                        "0"                        
[4] "Chicago Cardinals"        "1945"                     "1959"                     "66"                       
    "107"                      "4"                        ".382"                     "2"                        
    "1"                        "1"                        ""                         "1"                        
    "0"                        "0"                        "2"

Any help is greatly appreciated!

CodePudding user response:

Convert to matrix and then split by column:

asplit(matrix(all_teams, nrow = 15), 2)

With data:

asplit(matrix(1:60, nrow = 15), 2)

output:

[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

[[2]]
 [1] 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

[[3]]
 [1] 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

[[4]]
 [1] 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

CodePudding user response:

We can use split with gl to create a grouping which should work even for lengths that are not multiples of 15

split(all_teams, as.integer(gl(length(all_teams), 15, length(all_teams))))
  • Related