R: randomize the order of one data column.

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 create a DUAL random framework based on df1. For each of the random frames, I expect column A and column B to remain the same. But only the order of column C can be changed.

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

Many thanks.

+5
source share
2 answers

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

df2 <- transform( df1, C = sample(C) )
+14
source

You can do something like:

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

, , A B A B C, C sample. , , df2 df3.

+3

All Articles