How can R loop over data frames?

后端 未结 2 1198
南旧
南旧 2020-12-05 12:03

Suppose there are many data frames that need the same operation performed on them. For example:

prefix <- c(\"Mrs.\",\"Mrs.\",\"Mr\",\"Dr.\",\"Mrs.\",\"M         


        
2条回答
  •  孤街浪徒
    2020-12-05 12:38

    Put all your data frames into a list, and then loop/lapply over them. It'll be much easier on you in the long run.

    dfList <- list(df1=df1, df2=df2, ....)
    
    dfList <- lapply(dfList, function(df) {
        df$gender[df$prefix == "Mrs."] <- "F"
        df
    })
    
    dfList$df1
    

提交回复
热议问题