Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For loop R create and populate new column with output

I've got a csv with some results. I want to loop over the results, run a power.prop.test and output the result for each row in a new column. So far I've got this:

data <- read.csv("Downloads/data.csv", sep = ",", header = TRUE)

for (i in 1:nrow(data)) {
  n <- power.prop.test( p1 = data[i,5], p2 = data[i,6], sig.level=.1, power = .8, alternative = "one.sided")
  data <- cbind(data, n[1])
}
head(data)

Rather than populating one column with the output, I'm looping through and creating a new column for ever power.prop.test I'm running. I'm binding a new column for each output instead of populating one column with each output. Issue is I'm not sure how to achieve the latter.

If anyone has any advice on how to consolidate these outputs into one column that would be great.

Thanks!

like image 631
user2471446 Avatar asked Jul 07 '14 20:07

user2471446


2 Answers

Try this:

data <- read.csv("Downloads/data.csv", sep = ",", header = TRUE)

data$newcolumn <- 0

for (i in 1:nrow(data)) {
  n <- power.prop.test( p1 = data[i,5], p2 = data[i,6], sig.level=.1, power = .8, alternative = "one.sided")
  data$newcolumn[i] <- n
}
head(data)

I just added a new column, filled it with zeroes, and then added in the power.prop.test values one at a time as they are calculated.

like image 186
rucker Avatar answered Oct 21 '22 10:10

rucker


Thanks for all the help! Here's the solution I'm using for now:

# Read in csv of results
data <- read.csv("Downloads/data.csv")
data$obs=0
data$obs2=0
data$sig=0

# Create a loop to calculate required observations for each test based on CR,     Significance and Statistical Power
for (i in 1:nrow(data)) {
# Error handling: where CR are the same, cannot calculate n and fails, we skip these tests
  possibleError <- tryCatch(
      n <- power.prop.test( p1 = data[i,5], p2 = data[i,6], sig.level=.2, power = .8, alternative = "one.sided")
  ,error=function(e)e
  )
  if(!inherits(possibleError, "error")){
# Real work: calculate n, determine if bigger than actual observations, if not assign   sig = 0, otherwise =1
   n <- power.prop.test( p1 = data[i,5], p2 = data[i,6], sig.level=.2, power = .8,  alternative = "one.sided")
    data[i,'obs'] = n[[1]]
    data$obs2[i] = data$obs[i] * 2
    if(data$obs[i]*2 < data$Traffic[i]) {
      data$sig[i] <- 1
    } else {
      data$sig[i] <-0
    }
    }
    }    
# End for loop

write.csv(data,file = "dataClean.csv")

This runs the power.prop.test on each row, includes error handling and a condition to tell you whether you have enough observations. I'm sure there are more efficient ways to write this, so I'll review your comments to see if I can incorporate them.

like image 45
user2471446 Avatar answered Oct 21 '22 08:10

user2471446