My program takes a data.frame and crunches the numbers. At one point, values from j-th column are multiplied by a predefined values that depends on the column name (species name, actually – it’s en ecological index). So far, I’ve been providing these values via a second data.frame by matching column names. What would be an efficient way of integrating fixed variable values within a function? I would like my program to be as portable as possible, without the need for a second data.frame file.
EDIT
This is the function. I’m trying to improve the second line (index <- read.table…) so that it would not depend on the outside source.
macroIndex <- function(obj, index) {
index <- read.table("conv.csv", header=T, dec=",")
a <- c()
b <- names(obj)
for (i in 2:length(obj)) {
obj[i] <- obj[i] * index[which(index==b[i]), 2]
}
obj
}
Another solution I tried, while it may not seem pretty, it gets the job done. I use dput(index) and create a permanent object which I then insert into my function.
Well, you need to map your column names to another value, so you have to store it somehow. I would say that a named list would be a more appropriate data structure, although at the end of the day it doesn’t make a big difference.
Here’s some sample data:
Here’s a simple example of using the list:
Regarding Tal’s recommendation for using a matrix: that is true so long as every value in your data frame is of the same type. If you have mixed types, then you need to stick with a data frame.