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

12,541

Solution 1

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) )

Solution 2

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.

Share:
12,541
a83
Author by

a83

Updated on June 11, 2022

Comments

  • a83
    a83 almost 2 years

    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.

  • Grega Kešpret
    Grega Kešpret almost 13 years
    Very elegant! Didn't know about transform. Deserves a vote up :)
  • jkenney9
    jkenney9 over 7 years
    Brilliant! Love it!