Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a new column filled with random numbers

Tags:

r

Sorry for this simple question, I haven't found a fitting solution for this in my search. I'd like to create a new column in my data frame and fill it with random numbers between 1 to 100 (can repeat).

Below is the code I'm currently using,

data$newrow <- rep(1:100,replace=T, nrow(data))

I receive this error:

Error in `$<-.data.frame`(`*tmp*`, "newrow", value = c(1L, 2L,  : 
  replacement has 2088800 rows, data has 20888`

Can you help me fix my code?

like image 208
Mengll Avatar asked Aug 26 '13 13:08

Mengll


1 Answers

data$newrow <- sample(100, size = nrow(data), replace = TRUE)
like image 99
Thierry Avatar answered Oct 25 '22 00:10

Thierry