vendredi 9 septembre 2016

Storing the output from a loop as a list in R

I am running a small loop to randomly assign a list of numbers (1 to 30) to a subset of 4 groups. I would like to store the outputs of the loop (for 4 subsets) as a single line in one variable and use the results elsewhere. I am also getting some warnings, though the output is correctly displayed on the screen.

list = as.vector(c(6, 9, 3, 12)
start <- 1
end <- 6
i <- 1
while(i<=list){
  print(sample(start:end, replace=T))
  start <- start+list[i]
  end <- end + list[i+1]
  i <- i+1
}

[1] 3 5 6 1 5 6
[1]  9 13 12  7 11 12 14 11 14
[1] 16 17 17
 [1] 28 22 26 21 28 26 22 28 26 30 21 19
Error in start:end : NA/NaN argument
In addition: Warning messages:
1: In while (i <= list) { :
  the condition has length > 1 and only the first element will be used
2: In while (i <= list) { :
  the condition has length > 1 and only the first element will be used
3: In while (i <= list) { :
  the condition has length > 1 and only the first element will be used
4: In while (i <= list) { :
  the condition has length > 1 and only the first element will be used
5: In while (i <= list) { :
  the condition has length > 1 and only the first element will be used

I am unable to find the reasons for this error. Please help. Thanks.




Aucun commentaire:

Enregistrer un commentaire