Stored values ​​in a custom function

My program takes in data.frame and compresses the numbers. At some point, the values ​​from the j-th column are multiplied by predefined values ​​that depend on the column name (the species name, in fact, is the en ecological index). So far, I have provided these values ​​via a second data.frame by mapping the column names. What would be an efficient way to integrate fixed variable values ​​inside a function? I want my program to be as portable as possible, without having to use a second data.frame.

EDIT

This is a function. I am trying to improve the second line (index <- read.table ...) so that it doesn't depend on an external 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, although it might not seem very pretty, it gets the job done. I am using dput (index) and create a persistent object which I then insert into my function.

+2


a source to share


4 answers


Well, you need to map the column names to a different value, so you need to store it somehow. I would argue that a named list would be a more appropriate data structure, although it doesn't really matter at the end of the day.

Here are some sample data:

df <- data.frame(a=1:5, b=2:6)
mapping <- list(a=3, b=4)

      



Here's a simple example of using a list:

for(i in 1:ncol(df)) df[,i] <- df[,i] * mapping[[colnames(df)[i]]]

      

Regarding Tel's recommendation for using a matrix: this is true if every value in your dataframe is of the same type. If you have mixed types, you need to stick to the dataframe.

+1


a source


You can use the lexical definition of R to define a function function_maker

that returns the function you want func

. The code to create the display vector is only called when called function_maker

, not when func

. mapping

also belongs func

because other parts of your code cannot change it.



dat <- data.frame(a=c(1,2,3),b=c(3,2,0),c=c(5,6,4))

function_maker <- function(){
    mapping <- c(a=4,b=2,c=5)
    function(df){
        for(i in 1:ncol(df)) df[,i] <- df[,i] * mapping[[colnames(df)[i]]]
        return(df)
    }
}

func <- function_maker()

func(dat)

      

+1


a source


Why not include the second dataframe as a parameter to the function call and then check if it was provided, if not, create it manually so the code can work for datasets that match what you are currently doing , but you can change according to new datasets.

Something like (sorry I'm not on my PC so this is untested)

macroIndex <- function(obj, index) {
  if(!exists(index)) {
    index <- data.frame(# contents of the default data frame here )
  }
  a <- c()
  b <- names(obj)
  for (i in 2:length(obj)) {
      obj[i] <- obj[i] * index[which(index==b[i]), 2]
  }
  return(obj)
}

      

+1


a source


1) consider going to matrix instead of data.frame - to have faster results.

2) Coudl do you supply some simple code to explain what you want to achieve?

0


a source







All Articles