Create a new column filled with random numbers

Sorry for this simple question, I did not find a suitable solution for this in my search. I would like to create a new column in my data frame and fill it with random numbers from 1 to 100 (may repeat).

Below is the code I'm currently using,

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

I get 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?

+4
source share
1 answer
 data$newrow <- sample(100, size = nrow(data), replace = TRUE) 
+11
source

Source: https://habr.com/ru/post/1498961/


All Articles