简体   繁体   中英

Rewrite as a loop to set selected variables in similar dataframes as factors, in R

I have many years of data, each in an identical dataframe, I want to put all the years into a list and then write one for loop instead of repeating the command for each year.

#set dummies as factors (these dummies repeat across years)
mydummies<-c ('hru_i', 'ge_nonngsother_i','ge_sgt_i')
DF2012[,mydummies]<-lapply(DF2012[,mydummies],factor)
DF2013[,mydummies]<-lapply(DF2013[,mydummies],factor)

I tried to put all the dataframes in a list so that I could run a loop, but it did not change the dataframes...

df.list<- list(DF2012,DF2013)
#want to create a loop here

A nested lapply will work for this:

df.list <- lapply(df.list, function(d) {
  d[mydummies] <- lapply(d[mydummies], factor)
  d
})

Here's a reproducible example demonstrating that it works:

df.list = list(a = head(mtcars), b = head(mtcars))
mydummies = c("cyl", "am")
sapply(df.list, sapply, class)
#      a         b        
# mpg  "numeric" "numeric"
# cyl  "numeric" "numeric"
# disp "numeric" "numeric"
# hp   "numeric" "numeric"
# drat "numeric" "numeric"
# wt   "numeric" "numeric"
# qsec "numeric" "numeric"
# vs   "numeric" "numeric"
# am   "numeric" "numeric"
# gear "numeric" "numeric"
# carb "numeric" "numeric"

df.list <- lapply(df.list, function(d) {
  d[mydummies] <- lapply(d[mydummies], factor)
  d
})

#      a         b        
# mpg  "numeric" "numeric"
# cyl  "factor"  "factor" 
# disp "numeric" "numeric"
# hp   "numeric" "numeric"
# drat "numeric" "numeric"
# wt   "numeric" "numeric"
# qsec "numeric" "numeric"
# vs   "numeric" "numeric"
# am   "factor"  "factor" 
# gear "numeric" "numeric"
# carb "numeric" "numeric"

We can use tidyverse

library(dplyr)
library(purrr)
df.list <- map(df.list, ~ .x %>% 
                    mutate(across(all_of(mydummies), factor)))

data

df.list <- list(a = head(mtcars), b = head(mtcars))
mydummies <- c("cyl", "am")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM