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

Hello. Let's say I have a vector of variable names:

v <- c("var1","var2","var3")

And a data base that includes those variables. For example,

var1 <- c(1,2,3,4)
var2 <- c(3,4,5,6)
var3 <- c(5,6,7,8)
D <- data.frame (var1, var2, var3)

I need to use the names in vector v, in a for that uses database D.

Like this:

for (i in length(v)) {
tabulate(D$'i')
}

The questions is what would be the correct syntax to write such a task? As of now is not reading what comes in i as a variable name. In particular, how can I write these part:

D$'i'

I have tried also D$'i', and D$"i". But is not working.

Thank you for you help.

Actually the command should look like this:

for (i in length(v)) {
tabulate(D$'v{i]')
}

Here is where the assignation of the variable is not happenig.

Thanks

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
#> 
#> $disp
#>   [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#>  [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
#>  [75] 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
#> [112] 0 0 0 0 0 0 0 0 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0
#> [149] 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [186] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [223] 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
#> [260] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [297] 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [334] 0 1
#> 
#> $drat
#> [1]  0  3 22  7
#> 
#> $wt
#> [1]  4  8 16  1  3
#> 
#> $qsec
#>  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 4 9 7 3 3 0 1
#> 
#> $vs
#> [1] 14
#> 
#> $am
#> [1] 13
#> 
#> $gear
#> [1]  0  0 15 12  5
#> 
#> $carb
#> [1]  7 10  3 10  0  1  0  1

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

I prefer technocrats apply approach; but I'm writing this to introduce to you the double square bracket syntax, that can a) get a variable from a frame, b) return it in vector form ; looks likes D[["var2"]]

v <- c("var1","var2","var3")

D <- data.frame (var1=1:4,
                 var2=3:6,
                 var3=5:8)

  for (i in v) {
    print(i)
    print(tabulate(D[[i]]))
  }
1 Like

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.