I have a named list of data.frame objects (training_data). Each data.frame object will be tested based on a “x” column and if the test is passed, the name of that data.frame object is supposed to be returned.
In the case below, “a” and “b” are supposed to be returned:
df <- data.frame(x=1:10, y=1:10)
df1 <- data.frame(x=11:20, y=11:20)
training_data <- list(df, df, df1, df1)
names(training_data) <- c("a", "b", "c", "d")
pos <- lapply(training_data, function(data) {
if(data$x==1)
["return the name of the data.frame object in hand"]
})
My question is how exactly one can determine the name of the object currently being processed within any iteration of lapply and how to return that name so that it goes to “pos” list.
Regards
No need to use
lapplyhere, you can access your list for example :This will give you acess to the data.frame named ‘pos’ or posxx…
EDIT after OP clarification
I use lapply in the names of the list , I and I use the same mode of acces above to the main list
You can remove the NULL elements using something like
but I think you will get better output here if you use
sapply, which returns named list.