0

I'm still a bit of a novice R user, and I'm trying to run the following a number of times, each time storing a new variable into my data frame:

#Create data frame
ID <- c(286, 352, 477, 570, 870)
AV <- c(2, 3, 3, 2, 2)
AV2 <- c(1, 2, 2, 3, 3)
mydata <- data.frame(ID, AV, AV2)

Here's the key part that I want to set up in a loop:

mydata$NEWAV.1 <- mydata$AV 
switch <- sample(which(mydata$AV == 2), 45, replace = FALSE)
mydata.switch <- mydata[switch,]
mydata.stay <- mydata[-switch,]
mydata.switch$NEWAV.1 <- mydata.switch$AV2
mydata <- rbind(mydata.stay, mydata.switch)
mydata <- mydata[order(mydata$ID),]

My intent is to have a bunch of NEWAV.# variables in my data frame. So if I loop this over 100 times, I'd have NEWAV.1 to NEWAV.100 variables.

Here's my attempt. Unfortunately, I'm having trouble storing those new variables into the data frame.

#Doing it 3 times for testing purposes
for (i in 1:3){
switch <- sample(which(mydata$AV == 2), 45, replace = FALSE)
mydata.switch <- mydata[switch,]
mydata.stay <- mydata[-switch,]
cbind(assign(paste0("NEWAV.", i), mydata$AV), mydata)
cbind(assign(paste0("NEWAV.", i), mydata.switch$AV2), mydata.switch)
mydata <- rbind(mydata.stay, mydata.switch)
mydata <- mydata[order(mydata$ID),]
}
Phil
  • 7,287
  • 3
  • 36
  • 66
  • The third condition is a bit confusing. When you sample 15 cases, if any of those 15 that still fit condition 3 will be assigned to `VA`, then the rest will be assigned `VA2`? How do you get 1000 in the end? It seems that you start with 1000 rows and 3 columns and you are adding columns. The end result could be anything from 4 to 15003 columns, depending on how many rows meet condition 3. If thats the case, then all you have to do is calculate the probability of getting condition 3, then multiply that ny the probability of getting condition 3 out of picking 15 (`replace=FALSE`?). – N8TRO Aug 13 '15 at 06:06
  • How about a minimal reproducible example and expected output?! http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 – mts Aug 13 '15 at 06:10
  • @N8TRO I edited my question slightly to say "100 times" instead of "1000" since there seems to be confusion between the number of cases in my data and the number of times I wish to replicate the procedure. – Phil Aug 13 '15 at 16:13
  • Condition 3 is still unclear. Is this true: "When you sample 15 cases, if any of those 15 that still fit condition 3 will be assigned to VA, then the rest will be assigned VA2?" Please restate your condition 3 to be very explicit. Also, **very important**, without a [reproducible example & minimal data set](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610), (<--*follow this link*), AND and example output, (not big, emphasis on minimal to get the point), people here are MUCH less willing to help. – N8TRO Aug 13 '15 at 18:58
  • I've reformatted the question. Now I'm just presenting my existing code, and I'd like to have that looped. – Phil Aug 14 '15 at 16:48

0 Answers0