Steve
Steve

Reputation: 193

Data sets generation in R

I want to generate two data set for further calculation. The two sets are almost same except for one parameter. I would like to know is that possible to use loop to do this kind of automation in R. Thank you!

classes   <- c(0,1,2,3,4,5,6,7,8,9)

#this is parameter I would like to use to generate 2 different data set
n_1        <- 25
n_2        <- 50 

#This is data set I would like to get 
dataset_1 <- rep(classes, 25) 
dataset_2 <- rep(classes, 50) 

If I use n_pool as substitution for n_1 and n_2

n_pool         <- c(25,50)

#For loop has something wrong
for (a in n_pool) {
  dataset_[a] <- rep(classes, a)
}

I know the 'dataset_[a]' MUST be wrong, but I would like to know:

Is it possible that I write a loop in R to automate this process in avoid of any useless repetition?

Upvotes: 1

Views: 73

Answers (2)

ClancyStats
ClancyStats

Reputation: 1231

You're very close actually, you just need to use assign and paste0 like this:

classes   <- c(0,1,2,3,4,5,6,7,8,9)

#this is parameter I would like to use to generate 2 different data set
n_1        <- 25
n_2        <- 50 

#This is data set I would like to get 
dataset_1 <- rep(classes, 25) 
dataset_2 <- rep(classes, 50) 

n_pool         <- c(25,50)

#For loop has something wrong
for (a in n_pool) {
  assign(paste0('dataset_', a), rep(classes, a))
}

identical(dataset_1, dataset_25)
#> [1] TRUE
identical(dataset_2, dataset_50)
#> [1] TRUE

## or
for (i in 1:2) {
  assign(paste0('dataset_', i), rep(classes, get(paste0('n_', i))))
}
identical(dataset_1, dataset_25)
#> [1] TRUE
identical(dataset_2, dataset_50)
#> [1] TRUE

Created on 2019-10-02 by the reprex package (v0.3.0)

Upvotes: 0

www
www

Reputation: 39154

I think lapply is better than for-loop.

n_list <- list(n_1 = 25, n_2 = 50)

data_list <- lapply(n_list, function(x) rep(classes, x))

You can then access data by names, such as data_list$n_1.

Upvotes: 1

Related Questions