Andrew Hicks
Andrew Hicks

Reputation: 662

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

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.

Upvotes: 0

Views: 44

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389175

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)

Upvotes: 2

Related Questions