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.
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) )
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With