0
votes

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
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
In addition to the comment above, please specify the input: what is pyro_x, a vector or a data frame? Check out this guideline to learn about how to ask a great question.tonytonov
Thanks, i've updated the question and hopefully it's more understandable. Pyro_x is a vector.tastycanofmalk

1 Answers

2
votes

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)