J. OConnell
J. OConnell

Reputation: 13

For Loop and Table Printing in R

I'm trying to compute confidence intervals for many rows of a table using a for loop, and would like output that is more readable.. Here is a snippet of how the data looks.

       QUESTION X_YEAR X_PARTNER  X_CAMP       X_N      X_CODE1 
1     Q1    2011      SCSD           ITC    15       4      
2     Q1    2011      SCSD    Nottingham     4       1      
3     Q1    2011      SCSD           ALL    19       5      
4     Q1    2011        CP           CP1    18       4      
5     Q1    2011       ALL           ALL    37       9      
6     Q1    2012      SCSD           ITC     8       1      
7     Q1    2012      SCSD    Nottingham     8       2      
8     Q1    2012      SCSD           ALL    16       3      
9     Q1    2012        CP           CP1    18       2      
10    Q1    2012        CP           CP1    22       2      
11    Q1    2012        CP            ALL    40      4      

I'm trying to print out a confidence interval, with the Question, Year and Camp included. I'd like the output to be in table form like this

QUESTION   YEAR  CAMP         X   N  MEAN LOWER UPPER
Q1         2011  ITC          4   15  0.26 0.07 0.55
Q1         2011 NOTTINGHAM    1   4   0.25 0.006 0.8

with the first three columns being taken directly from the data table, and the latter 4 extracted from a confidence interval test I'm using.

The code I'm currently using:

for (i in 1:26){
    print(data[i,1],max.levels=0)
    print(data[i,2],max.levels=0)
    print(data[i,4],max.levels=0)
    print(binom.confint(data[i,6],data[i,5],conf.level=0.95,methods="exact"))
    }

provides output that (I have a lot more data than the snippet) will be far too time consuming to sift through...

[1] Q1
[1] 2011
[1] ITC
  method x  n      mean      lower     upper
1  exact 4 15 0.2666667 0.07787155 0.5510032
[1] Q1
[1] 2011
[1] Nottingham
  method x n mean       lower     upper
1  exact 1 4 0.25 0.006309463 0.8058796

Any advice is appreciated!

Upvotes: 1

Views: 976

Answers (1)

Rich Scriven
Rich Scriven

Reputation: 99331

If df is the name of your data, and you only want to do this for where QUESTION is Q1 (see comments), then

library(binom)
df2 <- df[df$QUESTION == "Q1",]
x <- vector("list", nrow(df2))
for(i in seq_len(nrow(df2))) {
    x[[i]] <- binom.confint(df2[i,6], df2[i,5], methods = "exact")
}
cbind(df2[c(1,2,4)], do.call(rbind, x)[,-1])
#    QUESTION X_YEAR     X_CAMP x  n       mean       lower     upper
# 1        Q1   2011        ITC 4 15 0.26666667 0.077871546 0.5510032
# 2        Q1   2011 Nottingham 1  4 0.25000000 0.006309463 0.8058796
# 3        Q1   2011        ALL 5 19 0.26315789 0.091465785 0.5120293
# 4        Q1   2011        CP1 4 18 0.22222222 0.064092048 0.4763728
# 5        Q1   2011        ALL 9 37 0.24324324 0.117725174 0.4119917
# 6        Q1   2012        ITC 1  8 0.12500000 0.003159724 0.5265097
# 7        Q1   2012 Nottingham 2  8 0.25000000 0.031854026 0.6508558
# 8        Q1   2012        ALL 3 16 0.18750000 0.040473734 0.4564565
# 9        Q1   2012        CP1 2 18 0.11111111 0.013751216 0.3471204
# 10       Q1   2012        CP1 2 22 0.09090909 0.011205586 0.2916127
# 11       Q1   2012        ALL 4 40 0.10000000 0.027925415 0.2366374

Note that conf.level = 0.95 is the default setting for binom.confint, so you don't need to include it in your call.

Upvotes: 1

Related Questions