pendermath
pendermath

Reputation: 190

how to use a loop in R with a non-numeric index

Given n data frames, say (n=3) dfa, dfb, dfpt, I would like to do the following:

dfa = dfa[, 'col1']
dfb = dfb[, 'col1']
dfpt = dfpt[, 'col1']

why writing one line of code, not n lines.

I have tried the following:

for(i in c('dfa', 'dfb', 'dfpt') {  df = df[, 'col1']  }

but I get an error message: incorrect number of dimensions.

Upvotes: 1

Views: 341

Answers (2)

akrun
akrun

Reputation: 887213

Here i is the object name as a string. We need get to extract the value of the object. Assuming that we are updating the original object, then use assign

for(i in c('dfa', 'dfb', 'dfpt')) assign(i, get(i)[, "col1"])

Upvotes: 2

Yuriy Saraykin
Yuriy Saraykin

Reputation: 8880

if I understand correctly

set.seed(1)
df1 <- data.frame(col1 = rnorm(10), col2 = rnorm(10))
df2 <- data.frame(col1 = rnorm(10), col2 = rnorm(10))
df3 <- data.frame(col1 = rnorm(10), col2 = rnorm(10))
ldf <- list(df1, df2, df3)

nm <- c("df1", "df2", "df3")
library(tidyverse)
map(ldf, ~.x[, "col1"]) %>% setNames(., nm)
#> $df1
#>  [1] -0.6264538  0.1836433 -0.8356286  1.5952808  0.3295078 -0.8204684
#>  [7]  0.4874291  0.7383247  0.5757814 -0.3053884
#> 
#> $df2
#>  [1]  0.91897737  0.78213630  0.07456498 -1.98935170  0.61982575 -0.05612874
#>  [7] -0.15579551 -1.47075238 -0.47815006  0.41794156
#> 
#> $df3
#>  [1] -0.1645236 -0.2533617  0.6969634  0.5566632 -0.6887557 -0.7074952
#>  [7]  0.3645820  0.7685329 -0.1123462  0.8811077

Created on 2021-02-27 by the reprex package (v1.0.0)

Upvotes: 2

Related Questions