0

I am trying to write a function that will randomly select about half of the experimental units stored in a vector x, assign them to treatment1 and treatment2. The output should display the entries of the vector that are assigned to each of the two treatments.

My codes seems to "work" but I am not sure if i should be using "matrix" or if i am actually even doing this correctly. My code is below:

myfunction <- function(x) {
      df.1 <- data.frame(matrix(x, nrow = length(x)/2))
      names(df.1) <- gsub("X", "Treatment", names(df.1), fixed = "TRUE")
      return(sample(df.1))
    }
  • Would `sample(x, length(x)/2)` not work? – Gopala Feb 26 '16 at 01:02
  • Yeah, that actually kind of worked. The only thing is that I need to have the output in a Table form. The first half under "Treatment1" and the second half under "Treatment2". What you told me just gives a horizontal listing of 15 random names. – user3061879 Feb 26 '16 at 01:07
  • Borrow something from here maybe - http://stackoverflow.com/questions/3318333/split-a-vector-into-chunks-in-r – thelatemail Feb 26 '16 at 01:14
  • 1
    You can try `t1 <- sample(1:length(x), length(x)/2); x_t1 <- x[t1]; x_t2 <- x[-t1]` – Gopala Feb 26 '16 at 01:32

1 Answers1

0

If I read this correctly, you want two equal sized data partitions on a data.frame. This is accomplished simply:

partition <- function(df, replace= FALSE, seed= 512L) {
  set.seed(seed)
  s <- sample(1:nrow(df), size= floor(nrow(df) / 2), replace= replace)
  return(list(df[s,], df[-s,]))
}

# example:
data(mtcars)
partition(mtcars)

Or for a vector:

partition.vec <- function(vec, replace= FALSE, seed= 512L) {
  set.seed(seed)
  s <- sample(1:length(vec), size= floor(length(vec) / 2), replace= replace)
  return(list(vec[s],vec[-s]))
}

# example:
v <- sample(1:100, size= 10)
partition.vec(v)
alexwhitworth
  • 4,839
  • 5
  • 32
  • 59