I have a list of numerical vectors. For each vector I want to determine the x-values for the kernal density peaks using a for loop. Running the for loop when printing the results works fine. When trying to store the results in a list I get the following error:
"Error in
[[<-(*tmp*, i, value = d$x[c(F, diff(diff(d$y) >= 0) < 0)]) : no such index at level 2"
The Error message in my original code
returns: "... no such index at level 1".
Can anyone help me fixing this error? The code for extracting the x-values for the kernal density peaks came from Calculating peaks in histograms or density functions, third answer.
set.seed(1234)
x <- list(col1 = c(rnorm(100, mean = 3), rnorm(100, mean = 4)),
col2 = c(rnorm(100, mean = 3), rnorm(100, mean = 4)))
# Works fine
output <- vector("list", length(x))
for (i in (x)){
d <- density(i)
d$x[c(F, diff(diff(d$y) >= 0) < 0)] %>% print()
}
# Does not work
output <- vector("list", length(x))
for (i in (x)){
d <- density(i)
d$x[c(F, diff(diff(d$y) >= 0) < 0)] -> output[[i]]
}