Emilie
Emilie

Reputation: 161

Randomizing a column in a list of dataframe

I want to have multiple copies of a dataframe, but with each time a new randomization of a variable. My objective behind this is to do multiple iterations of an analysis with a randomize value for one variable.

I've started by doing a list of dataframe, with copies of my original dataframe:

a <- c(1, 2, 3, 4, 5)
b <- c(45, 34, 50, 100, 64)
test <- data.frame(a, b)
test2 <- lapply(1:2,function(x) test) #List of 2 dataframe, identical to test

I know about transform and sample, to randomize the values of a column:

test1 <- transform(test, a = sample(a))

I just cannot find how to apply it to the entire list of dataframes. I've tried this:

test3<- lapply(test2,function(i) sample(i[["a"]]))

But I lost the other variables. And this:

test3 <- lapply(test2,function(i) {transform(i, i[["a"]]==sample(i[["a"]]))})

But my variable is not randomized.

Multiple questions are similar to mine, but didn't helped me to solve my problem:

Adding columns to each in a list of dataframes

Add a column in a list of data frames

Upvotes: 0

Views: 95

Answers (2)

user3818004
user3818004

Reputation:

Is there a reason you need them in separate lists?

This will give you 10 columns of randomized samples of a in different columns and then you could loop through the columns for your further analysis.

a <- c(1, 2, 3, 4, 5)
b <- c(45, 34, 50, 100, 64)
test <- data.frame(a, b)

for(i in 3:12){
test[,i] <- transform(sample(a))
}

`

Upvotes: 0

talat
talat

Reputation: 70246

You can try the following:

lapply(test2, function(df) {df$a <- sample(df$a); df})

Or, using transform:

lapply(test2, function(df) transform(df, a = sample(a)))

Or just

lapply(test2, transform, a = sample(a))

Upvotes: 1

Related Questions