Assign value of a vector as a variable name in a datafrema

Lacking a full a reprex (see the FAQ I won't work this out in full, but two points.

  1. Syntax for subsetting
mtcars$mpg    |> tail()
#> [1] 26.0 30.4 15.8 19.7 15.0 21.4
mtcars$"mpg"  |> tail()
#> [1] 26.0 30.4 15.8 19.7 15.0 21.4
mtcars["mpg"] |> tail()
#>                 mpg
#> Porsche 914-2  26.0
#> Lotus Europa   30.4
#> Ford Pantera L 15.8
#> Ferrari Dino   19.7
#> Maserati Bora  15.0
#> Volvo 142E     21.4
mtcars[1]     |> tail()
#>                 mpg
#> Porsche 914-2  26.0
#> Lotus Europa   30.4
#> Ford Pantera L 15.8
#> Ferrari Dino   19.7
#> Maserati Bora  15.0
#> Volvo 142E     21.4
mtcars[,1]    |> tail()
#> [1] 26.0 30.4 15.8 19.7 15.0 21.4

Created on 2023-09-13 with reprex v2.0.2

These all work, but in different ways—three of them return vectors and two of them return data frames. I prefer the last in all cases not requiring a data frame.

  1. Loops.

Rather than for i in length(v) use for(i in seq_along(v). But don't use for.

mtcars$mpg    |> str() 
#>  num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
mtcars$"mpg"  |> str()
#>  num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
mtcars["mpg"] |> str()
#> 'data.frame':    32 obs. of  1 variable:
#>  $ mpg: num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
mtcars[1]     |> str()
#> 'data.frame':    32 obs. of  1 variable:
#>  $ mpg: num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
mtcars[,1]    |> str()
#>  num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
apply(mtcars,2,tabulate)
#> $mpg
#>  [1] 0 0 0 0 0 0 0 0 0 2 0 0 1 2 5 1 2 2 3 0 5 2 0 1 0 1 1 0 0 2 0 1 1
#> 
#> $cyl
#> [1]  0  0  0 11  0  7  0 14
#>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