Home > Back-end >  How to use a for loop to extract columns from a data frame
How to use a for loop to extract columns from a data frame

Time:05-21

I am trying to use a for loop to extract columns from a dataframe (named table1) and create a new dataframe (named smalldata) with the latter having only these 3 columns (ID1, ID2, ID3). I have included my code below which does not work.

for (i in 1:3) {
  idlist[[i]] <- table1$ID[i]
}
smalldata <- do.call(cbind, idlist)
View(smalldata)

Can [i] be used with $ in a dataframe to extract these columns in the for loop?

CodePudding user response:

If you must do it with a for loop, you could work off this:

new <- list()      # construct as list -- data.frames are fancy lists
cols <- c(1, 5, 3) # use a vector of column indices
for (i in seq_along(cols)) {
  # append the list at each column
  new[[i]] <- mtcars[, cols[i], drop = FALSE]
}

new <- as.data.frame(new)      # make list into data.frame
identical(new, mtcars[, cols]) # check that this produces the same thing
#> [1] TRUE
head(new)
#>                    mpg drat disp
#> Mazda RX4         21.0 3.90  160
#> Mazda RX4 Wag     21.0 3.90  160
#> Datsun 710        22.8 3.85  108
#> Hornet 4 Drive    21.4 3.08  258
#> Hornet Sportabout 18.7 3.15  360
#> Valiant           18.1 2.76  225
str(new)
#> 'data.frame':    32 obs. of  3 variables:
#>  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#>  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
#>  $ disp: num  160 160 108 258 360 ...

Created on 2022-05-20 by the reprex package (v2.0.1)

CodePudding user response:

Sounds like a good use case for dplyr::select

library(dplyr)

# character vector of column names
vec_column_names <- c("Species", "Petal.Width")

df_small <- iris |> 
  select(all_of(vec_column_names))

# or a vector of column positions
df_small <- iris |> 
  select(1:3)
  • Related