0

I've experienced a similar problem more along the lines of being inefficient rather than than failing to function, it was solved here using lapply.

(Edited) I'm including working code with junk data this time that will throw the error back when run. The code below works as is, however I would like to replace the if (numruns ==... ) statements with the #'d lapply functions at the very end. The lapply using func_ign_time assigns NULL values. I'm also encountering some errors with the func_drop_time, but they may be related.

numruns = 3
pyro_1 <- as.numeric(c(100,70,50,2,3,4,60,160,260,360,503))
pyro_2 <- as.numeric(c(100,100,100,70,50,2,3,4,60,160,260,360,503))
pyro_3 <- as.numeric(c(100,100,70,50,2,3,4,60,160,260,360,503))
time_diff <- seq(1,100,1)

func_ign_time <- function(data, delay = 5, threshold = 5, time="time_diff"){
        diff_data <- diff(data[1:length(data)])
        flag <- 0
        for (i in 1:length(diff_data)){
                if (diff_data[i] > threshold && diff_data[i+1] > threshold && i > delay){
                        flag <- 1
                        time <- as.numeric(time_diff[i])
                        #print(paste0("Plug ignition time: ", time, " seconds"))
                        return (time)
                }
        }
}
func_drop_time <- function(data, threshold = -5, time="time_diff"){
        diff_data <- diff(data[1:length(data)])
        flag <- 0
        for (i in 1:length(diff_data)){
                if (diff_data[i] < threshold && flag == 0){
                        flag <- 1
                        time <- as.numeric(time_diff[i])
                        #print(paste0("Plug drop time: ", time, " seconds"))
                        return(time)
                }
        }
}

if (numruns == 3){
        time_ign_3 <- as.numeric(func_ign_time(pyro_3)-func_drop_time(pyro_3))
        time_ign_2 <- as.numeric(func_ign_time(pyro_2)-func_drop_time(pyro_2))
        time_ign_1 <- as.numeric(func_ign_time(pyro_1)-func_drop_time(pyro_1))
        print(paste(time_ign_1,time_ign_2,time_ign_3))
}
if (numruns == 2){
        time_ign_2 <- as.numeric(func_ign_time(pyro_2)-func_drop_time(pyro_2))
        time_ign_1 <- as.numeric(func_ign_time(pyro_1)-func_drop_time(pyro_1))
        print(paste(time_ign_1,time_ign_2))
}
if (numruns == 1){
        time_ign_1 <- as.numeric(func_ign_time(pyro_1)-func_drop_time(pyro_1))
        print(paste(time_ign_1))
}

#ign_names <- paste0("pyro_", seq_len(numruns))
#xx <- lapply(ign_names, function(x) (func_ign_time(x)))
#yy <- lapply(ign_names, function(x) (func_drop_time(x)))
#zz <- xx-yy
Jaap
  • 81,064
  • 34
  • 182
  • 193
tastycanofmalk
  • 628
  • 7
  • 23
  • 1
    The context here is a little unclear; it would help if you could edit your question so that the code can run on its own. Initially, I see that you're using `lapply` to call `func_ing_time`, but the `return` value of `func_ing_time` is nested within a loop. You can only return one value from a function; are you expecting the `func_ing_time` function to return multiple values? If so, then you should replace the `for` loop within the func_ing_time with a call to `lapply` or `sapply`. Again, it would be easier to help you if you posted code that runs. – FascinatingFingers Apr 24 '14 at 17:39
  • In addition to the comment above, please specify the input: what is `pyro_x`, a vector or a data frame? Check out [this guideline](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to learn about how to ask a great question. – tonytonov Apr 24 '14 at 17:50
  • Thanks, i've updated the question and hopefully it's more understandable. Pyro_x is a vector. – tastycanofmalk Apr 24 '14 at 19:00

1 Answers1

2

You're passing a char variable to func_ign_time(...), when it expects a vector (apparently). In other words, you're doing the equivalent of:

func_ign_time("pyro_1")

when you want

func_ign_time(pyro_1)

Try this instead:

lst <- list(pyro_1, pyro_2,pyro_3)
lapply(ign_names, func_ign_time)

Alternatively, try:

lapply(mget(ign_names), func_ign_time)
jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • Thanks for the reply. This actually works when I use a list with the names pyro_1-3. Unfortunately this dances around the original problem which was to automate the population of a list that contains e.g. pyro_1-3, when numruns ==3, pyro_1-2, when numruns ==2, etc – tastycanofmalk Apr 24 '14 at 18:13
  • `lapply(mget(ign_names)[1:numruns], func_ign_time)` – jlhoward Apr 24 '14 at 18:17
  • Works! I'm sorry I don't completely understand it yet, but I've gotten the return values I believe I want. `(x<-as.numeric(lapply(mget(ign_names)[1:numruns], func_ign_time))` `y<-as.numeric(lapply(mget(ign_names)[1:numruns], func_drop_time))` `z<-x-y)` – tastycanofmalk Apr 24 '14 at 19:10