1

My dataset looks like this, and I have a list of data.

   Plot_ID Canopy_infection_rate DAI 
1  YO01    5                     7   
2  YO01    8                     14
3  YO01    10                    21

What I want to do is to apply a function called "audpc_Canopyinfactionrate" to a list of dataframes.

However, when I run lapply, I get an error as below:

Error in FUN(X[[i]], ...) : argument "DAI" is missing, with no default

I've checked my list that my data does not shift a column.

Does anyone know what's wrong with it? Thanks

Here is part of my code:

#Read files in to list

for(i in 1:length(files)) {
  lst[[i]] <- read.delim(files[i], header = TRUE,  sep=" ")
}

#Apply a function to the list
densities <- list()
densities<- lapply(lst, audpc_Canopyinfactionrate)

#canopy infection rate 
audpc_Canopyinfactionrate <- function(Canopy_infection_rate,DAI){
  n <- length(DAI)
  meanvec <- matrix(-1,(n-1))
  intvec <- matrix(-1,(n-1))
  for(i in 1:(n-1)){
    meanvec[i] <- mean(c(Canopy_infection_rate[i],
                         Canopy_infection_rate[i+1]))
    intvec[i] <- DAI[i+1] - DAI[i]
  }

  infprod <- meanvec * intvec
  sum(infprod)

}
4
  • You didn't specify a value for the DAI= argument to the audpc_Canopyinfactionrate() function. Hence the error message: argument "DAI" is missing, with no default. Actually, it doesn't look like you've passed any arguments to the function. Commented Sep 19, 2017 at 2:22
  • Thanks. This is actually a function from agricolae. if I run in single data frame like this : audpc_Canopyinfactionrate(X17YO1001$Canopy_infection_rate,X17YO1001$DAI). It works fine. Commented Sep 19, 2017 at 2:43
  • 1
    The source of the function won't make a difference here I don't think. You've essentially done f <- function(x,y) x+y; lapply(1:3, f) - you need to pass all the required arguments in to the function to avoid the error - f <- function(x,y) x+y; lapply(1:3, f, y=2:4) for instance. Commented Sep 19, 2017 at 2:48
  • Could you please vote up/accept KenHBS answer as it solved your issue? Thank you. Commented May 15, 2019 at 6:12

1 Answer 1

2

As pointed out in the comments, the problem lies in the way you are using lapply.

This function is built up like this: lapply(X, FUN, ...). FUN is the name of a function used to apply to the elements in a data.frame/list called X. So far so good.

Back to your case: You want to apply a function audpc_Canopyinfactionrate() to all data frames in lst. This function takes two arguments. And I think this is where things got mixed up in your code. Make sure you understand that in the way you are using lapply, you use lst[[1]], lst[[2]], etc. as the only argument in audpc_Canopyinfactionrate(), whereas it actually requires two arguments!

If you reformulate your function a bit, you can use lst[[1]], lst[[2]] as the only argument to your function, because you know that argument contains the columns you need - Canopy_infection_rate and DAI:

audpc_Canopyinfactionrate <- function(df){
  n <- nrow(df)
  meanvec <- matrix(-1, (n-1))
  intvec  <- matrix(-1, (n-1))
  for(i in 1:(n-1)){
    meanvec[i] <- mean(c(df$Canopy_infection_rate[i],
                         df$Canopy_infection_rate[i+1]))
    intvec[i] <- df$DAI[i+1] - df$DAI[i]
  }

  infprod <- meanvec * intvec
  return(sum(infprod))    
}

Call lapply in the following way:

lapply(lst, audpc_Canopyinfactionrate)

Note: lapply can also be used with more than 1 argument, by using the ... in lapply(X, FUN, ...). In your case, however, I think this is not the best option.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.