ANam
ANam

Reputation: 347

How to access an object using relative reference to name in list

I am trying to essentially split a dataframe that I have into several different dataframes that are each just one column of the original dataframe and store each of these in a list. I am naming each of these new dataframes with the original column names but don't know how to access the object (dataframe) instead of simply the name of the column. Simple example below:

tablenames <- names(mtcars)

for (i in 1:3) {
  assign(paste0(tablenames[i]), mtcars %>% select(paste0(tablenames[i])))
}

tablenames[i]
>>"disp"

Instead of "disp", I want to return the object disp that is created (but without actually typing out disp). Is this possible?

Upvotes: 0

Views: 134

Answers (2)

ANam
ANam

Reputation: 347

@JasonAizkalns answer does work but I've found a simpler resolution using the get() function.

tablenames <- names(mtcars)
tables <- list()

for (i in 1:3) {
  assign(paste0(tablenames[i]), mtcars %>% dplyr::select(paste0(tablenames[i])))
  tables[[i]] <- get(tablenames[i])
  rownames(tables[[i]]) <- c()
} 

tables[[2]] 

This allows me to create a list of dataframes instead of vectors which is what I needed.

Upvotes: 0

JasonAizkalns
JasonAizkalns

Reputation: 20473

I think this might be what you are going after:

library(dplyr)
tablenames <- vector('list', length = length(names(mtcars)))
tablenames <- setNames(tablenames, names(mtcars))

for (i in 1:3) {
  tablenames[i] <- mtcars %>% select(names(tablenames[i]))
}

str(tablenames)
#> List of 11
#>  $ mpg : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#>  $ cyl : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
#>  $ disp: num [1:32] 160 160 108 258 360 ...
#>  $ hp  : NULL
#>  $ drat: NULL
#>  $ wt  : NULL
#>  $ qsec: NULL
#>  $ vs  : NULL
#>  $ am  : NULL
#>  $ gear: NULL
#>  $ carb: NULL
tablenames[3]
#> $disp
#>  [1] 160.0 160.0 108.0 258.0 360.0 225.0 360.0 146.7 140.8 167.6 167.6
#> [12] 275.8 275.8 275.8 472.0 460.0 440.0  78.7  75.7  71.1 120.1 318.0
#> [23] 304.0 350.0 400.0  79.0 120.3  95.1 351.0 145.0 301.0 121.0

Created on 2019-09-09 by the reprex package (v0.3.0)

See also tablenames[[3]].

A better approach, however, could be to leverage tibble::enframe and purrr::map:

library(tidyverse)

mtcars %>%
  map(enframe, name = NULL)
#> $mpg
#> # A tibble: 32 x 1
#>    value
#>    <dbl>
#>  1  21  
#>  2  21  
#>  3  22.8
#>  4  21.4
#>  5  18.7
#>  6  18.1
#>  7  14.3
#>  8  24.4
#>  9  22.8
#> 10  19.2
#> # ... with 22 more rows
#> 
#> $cyl
#> # A tibble: 32 x 1
#>    value
#>    <dbl>
#>  1     6
#>  2     6
#>  3     4
#>  4     6
#>  5     8
#>  6     6
#>  7     8
#>  8     4
#>  9     4
#> 10     6
#> # ... with 22 more rows
#> 
#> $disp
#> # A tibble: 32 x 1
#>    value
#>    <dbl>
#>  1  160 
#>  2  160 
#>  3  108 
#>  4  258 
#>  5  360 
#>  6  225 
#>  7  360 
#>  8  147.
#>  9  141.
#> 10  168.
#> # ... with 22 more rows
#> 
#> $hp
#> # A tibble: 32 x 1
#>    value
#>    <dbl>
#>  1   110
#>  2   110
#>  3    93
#>  4   110
#>  5   175
#>  6   105
#>  7   245
#>  8    62
#>  9    95
#> 10   123
#> # ... with 22 more rows
#> 
#> $drat
#> # A tibble: 32 x 1
#>    value
#>    <dbl>
#>  1  3.9 
#>  2  3.9 
#>  3  3.85
#>  4  3.08
#>  5  3.15
#>  6  2.76
#>  7  3.21
#>  8  3.69
#>  9  3.92
#> 10  3.92
#> # ... with 22 more rows
#> 
#> $wt
#> # A tibble: 32 x 1
#>    value
#>    <dbl>
#>  1  2.62
#>  2  2.88
#>  3  2.32
#>  4  3.22
#>  5  3.44
#>  6  3.46
#>  7  3.57
#>  8  3.19
#>  9  3.15
#> 10  3.44
#> # ... with 22 more rows
#> 
#> $qsec
#> # A tibble: 32 x 1
#>    value
#>    <dbl>
#>  1  16.5
#>  2  17.0
#>  3  18.6
#>  4  19.4
#>  5  17.0
#>  6  20.2
#>  7  15.8
#>  8  20  
#>  9  22.9
#> 10  18.3
#> # ... with 22 more rows
#> 
#> $vs
#> # A tibble: 32 x 1
#>    value
#>    <dbl>
#>  1     0
#>  2     0
#>  3     1
#>  4     1
#>  5     0
#>  6     1
#>  7     0
#>  8     1
#>  9     1
#> 10     1
#> # ... with 22 more rows
#> 
#> $am
#> # A tibble: 32 x 1
#>    value
#>    <dbl>
#>  1     1
#>  2     1
#>  3     1
#>  4     0
#>  5     0
#>  6     0
#>  7     0
#>  8     0
#>  9     0
#> 10     0
#> # ... with 22 more rows
#> 
#> $gear
#> # A tibble: 32 x 1
#>    value
#>    <dbl>
#>  1     4
#>  2     4
#>  3     4
#>  4     3
#>  5     3
#>  6     3
#>  7     3
#>  8     4
#>  9     4
#> 10     4
#> # ... with 22 more rows
#> 
#> $carb
#> # A tibble: 32 x 1
#>    value
#>    <dbl>
#>  1     4
#>  2     4
#>  3     1
#>  4     1
#>  5     2
#>  6     1
#>  7     4
#>  8     2
#>  9     2
#> 10     4
#> # ... with 22 more rows

Upvotes: 2

Related Questions