How to shuffle the rows of the data set

r

#1

Currently I working on a data set and while working on it I found that that my factor variable is ordered I want to shuffle it so that I can understand my data in better way to create any classification model.

head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

here a Species value is setosa for first six rows and I want to shuffle it so that all Species can cover in first six rows.


#2

If your trying to remove the order from the variables, this should work :

x <- factor( x , ordered = FALSE )

Let me know if it doesn’t work.


#3

df.reindex(np.random.permutation(df.index))

Hope this works!


#4

df1 <- data.frame(
a = c(1,1,0,0) ,
b = c(1,0,1,0),
c = c(0,0,0,0)
)

df2 <- df1[sample(nrow(df1)),]