I have started using data.table
for a large population model. So far, I have been impressed because using the data.table structure decreases my simulation run times by about 30%. I am trying to further optimize my code and have included a simplified example. My two questions are:
- Is is possible to use the
:=
operator with this code?
- Would using the
:=
operator be quicker (although, if I am able to answer my first question, I should be able to answer my question 2!)?
I am using R version 3.1.2 on a machine running Windows 7 with data.table
version 1.9.4.
Here is my reproducible example:
library(data.table)
## Create example table and set initial conditions
nYears = 10
exampleTable = data.table(Site = paste("Site", 1:3))
exampleTable[ , growthRate := c(1.1, 1.2, 1.3), ]
exampleTable[ , c(paste("popYears", 0:nYears, sep = "")) := 0, ]
exampleTable[ , "popYears0" := c(10, 12, 13)] # set the initial population size
for(yearIndex in 0:(nYears - 1)){
exampleTable[[paste("popYears", yearIndex + 1, sep = "")]] <-
exampleTable[[paste("popYears", yearIndex, sep = "")]] *
exampleTable[, growthRate]
}
I am trying to do something like:
for(yearIndex in 0:(nYears - 1)){
exampleTable[ , paste("popYears", yearIndex + 1, sep = "") :=
paste("popYears", yearIndex, sep = "") * growthRate, ]
}
However, this does not work because the paste does not work with the data.table
, for example:
exampleTable[ , paste("popYears", yearIndex + 1, sep = "")]
# [1] "popYears10"
I have looked through the data.table documentation. Section 2.9 of the FAQ uses cat
, but this produces a null output.
exampleTable[ , cat(paste("popYears", yearIndex + 1, sep = ""))]
# [1] popYears10NULL
Also, I tried searching Google and rseek.org, but didn't find anything. If am missing an obvious search term, I would appreciate a search tip. I have always found searching for R operators to be hard because search engines don't like symbols (e.g., ":=
") and "R" can be vague.
See Question&Answers more detail:
os