Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: randomize order of one column of a data.frame

I have a dataframe like this:

df1 <- data.frame(A=c("xx", "be", "zz", "jj"), B=c("xyx", "bea", "cce", "ggg"), C=c("ges", "xyz", "cce", "edga"))

I want to generate TWO random dataframe based on df1. For each of the random dataframe, I expect the column A and column B remains the same. But only the order of column C can be altered.

Can I do it with R? If yes, could you teach me how to do so?

Thanks a lot.

like image 954
a83 Avatar asked May 31 '11 05:05

a83


2 Answers

When creating a new data-frame based on an existing one, the usual paradigm in R is to use transform. In your case you can simply do:

df2 <- transform( df1, C = sample(C) )
like image 67
Prasad Chalasani Avatar answered Sep 19 '22 17:09

Prasad Chalasani


You could do something like:

data.frame(A=df1$A, B=df1$B, C=sample(df1$C))

Thus, creating a new data frame where A and B are old data frame's A and B and C is a random permutation of old data frame's column C by using a sample command. Of course, you would assign this new data frame a variable, like df2 and df3.

like image 41
Grega Kešpret Avatar answered Sep 22 '22 17:09

Grega Kešpret