2

I'm new to R, and new to this forum. I've searched but cannot easily find an answer to this question:

I have numbers of cases of a disease by week according to location, stored in a .csv file with variable names cases.wk24, cases.wk25, etc. I also have population for each location, and want to generate incidence rates (# cases/population) for each of the locations.

I would like to write a loop that generates incidence rates by location for each week, and stores these in new variables called "ir.wk24", "ir.wk25", etc

I am stuck at 2 points:

  1. is it possible to tell R to run a loop if it comes across a variable that looks like "cases.wk"? In some programmes, one would use a star - cases.wk*

  2. How could I then generate the new variables with sequential naming and store these in the dataset?

I really appreciate any help on this - been stuck with internet searches all day!

thanks

Jonny
  • 2,703
  • 2
  • 27
  • 35
  • Take a look here: http://stackoverflow.com/q/5186570/602276 or here http://stackoverflow.com/q/2593412/602276 or here http://stackoverflow.com/q/2209258/602276, all of which I found in 2 minutes after searching StackOverflow for `[r] loop csv` – Andrie Jul 20 '12 at 16:00
  • 1
    Show us a bit of your data, or something that looks a bit like your data, and then show us what you want to get from it. Normally in R you would avoid creating new variables in a loop and make a structure with everything in. Or operate on a whole matrix. Loops are bad, mm'kay? – Spacedman Jul 20 '12 at 16:00
  • Check out http://stackoverflow.com/questions/2679193/how-to-name-variables-on-the-fly-in-r – Brian Diggs Jul 20 '12 at 16:08

1 Answers1

0
x <- data.frame(case.wk24=c(1,3),case.wk25=c(3,2), pop=c(7,8))

weeks <- 24:25
varnames <- paste("case.wk", weeks, sep="")
ir <- sapply(varnames,FUN=function(.varname){
    x[,.varname]/x[,"pop"]
})
ir <- as.data.frame(ir)
names(ir) <- paste("ir.wk", weeks, sep="")
x <- cbind(x,ir)
x
Michael
  • 5,808
  • 4
  • 30
  • 39