lundi 31 juillet 2017

Sequentially re-ordering sections of a vector around NA values

I have a large set of data that I want to reorder in groups of twelve using the sample() function in R to generate randomised data sets with which I can carry out a permutation test. However, this data has NA characters where data could not be collected and I would like them to stay in their respective original positions when the data is shuffled.

With help on a previous question I have managed to shuffle the data around the NA values for a single vector of 24 values with the code:

    example.data <- c(0.33, 0.12, NA, 0.25, 0.47, 0.83, 0.90, 0.64, NA, NA, 1.00, 0.42)

    example.data[!is.na(example.data)] <- sample(example.data[!is.na(example.data)], replace = F, prob = NULL)

[1] 0.64  0.83  NA  0.33  0.47  0.90  0.25  0.12  NA  NA  0.42  1.00

Extending from this, if I have a set of data with a length of 24 how would I go about re-ordering the first and second set of 12 values as individual cases in a loop?

For example, a vector extending from the first example:

example.data <- c(0.33, 0.12, NA, 0.25, 0.47, 0.83, 0.90, 0.64, NA, NA, 1.00, 0.42, 0.73, NA, 0.56, 0.12, 1.0, 0.47, NA, 0.62, NA, 0.98, NA, 0.05)

Where example.data[1:12] and example.data[13:24] are shuffled separately within their own respective groups around their NA values.

The code I am trying to work this solution into is as follows:

shuffle.data = function(input.data,nr,ns){
simdata <- input.data
  for(i in 1:nr){
    start.row <- (ns*(i-1))+1
    end.row   <- start.row + actual.length[i] - 1
    newdata = sample(input.data[start.row:end.row], size=actual.length[i], replace=F)
    simdata[start.row:end.row] <- newdata
      }
return(simdata)}

Where input.data is the raw input data (example.data); nr is the number of groups (2), ns is the size of each sample (12); and actual.length is the length of each group exluding NAs stored in a vector (actual.length <- c(9, 8) for the example above).

Would anyone know how to go about achieving this?

Thank you again for your help!




Aucun commentaire:

Enregistrer un commentaire