generate a column of random numbers

463 Views Asked by At

I wanted to generate a column of random numbers in R taking values between 1 and 371, but I get an error I am not able to fix.

here is the code

library(readstata13)

#load data sets
data1<- read.dta13("file1.dta", convert.factors = FALSE, select.cols=(c("var1") ))

data2<- read.dta13("file2.dta", convert.factors = FALSE, select.cols=(c("var2") ))

finaldataset <-rbindlist(list(data1, data2),idcol=TRUE)

set.seed(123456789)

finaldataset$lad<-sample(371)

this is the error message I get

Error in set(x, j = name, value = value) : 
  Supplied 371 items to be assigned to 36137 items of column 'lad'. If you wish to 'recycle' the RHS please use rep() to make this intent clear to readers of your code.

Any help would be much appreciated, thanks

1

There are 1 best solutions below

2
David Clarke On

I cannot load your data and therefore examine it, however, it seems as though you are trying to create a column that is not the same length as the columns in your finaldataset. Just using sample(371) will only create 371 values, therefore, you will need to specify how many values to create i.e. a size that is the same length as your other data.