Is there an easy way to simplify this code using a loop?

最后都变了- 提交于 2021-01-29 09:50:44

问题


Is there a way to simplify this code using a loop?

set.seed(100) 

AL_INDEX <- sample(1:nrow(AL_DF), 0.7*nrow(AL_DF))
AL_TRAIN <- AL_DF[AL_INDEX,]
AL_TEST <- AL_DF[-AL_INDEX,]  

AR_INDEX <- sample(1:nrow(AR_DF), 0.7*nrow(AR_DF))
AR_TRAIN <- AR_DF[AR_INDEX,]
AR_TEST <- AR_DF[-AR_INDEX,]  

AZ_INDEX <- sample(1:nrow(AZ_DF), 0.7*nrow(AZ_DF))
AZ_TRAIN <- AZ_DF[AZ_INDEX,]
AZ_TEST <- AZ_DF[-AZ_INDEX,]  

AL_DF, AR_DF & AZ_DF are data frames that have the same field structure, but different number of records.


回答1:


Find a pattern to capture all the dataframe names. In the example shared all of them end with "_DF", use mget to get them in list. Divide the data in test and train and unlist them one level.

data <- unlist(lapply(mget(ls(pattern = '_DF$')), function(df) {
            index <- sample(1:nrow(df), 0.7*nrow(df))
            list(train = df[index,], test = df[-index,])  
         }), recursive = FALSE)

Now get them into individual dataframes using list2env.

list2env(data, .GlobalEnv)


来源:https://stackoverflow.com/questions/62055439/is-there-an-easy-way-to-simplify-this-code-using-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!