[R] Create factorial design
    Sascha Vieweg 
    saschaview at gmail.com
       
    Sat Feb  5 11:01:33 CET 2011
    
    
  
I have got data with one column indicating the area where the data 
was recorded:
R: n <- 43
R: df <- data.frame("area"=sample(1:7, n, repl=T), "dat"=rnorm(n))
In each of the 7 different areas I want to implement one of 7 
specific strategies. The assignment should be random. Therefore, I 
pair 7 areas with 7 strategies randomly by
R: ass <- as.data.frame(cbind("area"=sample(1:7, 7),
    "strategy"=sample(1:7, 7)))
Now I want to create a new variable indicating, which case in the 
original data should be assigned to which strategy. I thought 
about
R: x <- numeric(n)
R: for(i in 1:7){
      x[df[, "area"]==i] <- ass[ ass[, "area"]==i , "strategy"]
    }
and then binding the new variable to the data frame
R: str(df2 <- as.data.frame(cbind(df, "strategy"=x)))
which works fine. My question is whether there is a more elegant 
way?
Thanks, *S*
-- 
Sascha Vieweg, saschaview at gmail.com
    
    
More information about the R-help
mailing list