0

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

Wimpel
  • 26,031
  • 1
  • 20
  • 37

1 Answers1

0

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.

  • Thank you, I've actually used another command that specified the number of rows in the dataset by using this command finaldataset$lad <- floor(runif(n=36137, min=1, max=372)) and it worded weel. However, I was wondering whether there is any workaround without the need to include the exact number of rows. – Agnese Romiti Jun 03 '21 at 13:23
  • Perhaps you could try the line of code in the other comment, however, replace `$lad` with an existing column name e.g. if in `finaldataset` you had a column named `col1`, then use `finaldataset$lad <- sample(1:371, length(finaldataset$col1))` – David Clarke Jun 03 '21 at 22:16