I ran a loop that iterated through and created a bunch of objects whose names all start with "results_" and that are of differing nrow
lengths, many of which are 0.
To make this list of objects easier to handle, I'd like to remove any objects whose nrow
is equal to 0. I've tried below a variety of solutions provided for similar questions to this one, but none worked for my particular case. What am I doing wrong?
Attempt 1:
rm(list=ls(all=TRUE)[sapply(mget(ls(all=TRUE)), function(x) nrow(x) == 0)])
Attempt 2:
rm(list=ls()[!sapply(mget(ls(),.GlobalEnv), function(x) { nrow(x) == 0 } )])
rm(list=
Filter(
Negate(is.na), # filter entries corresponding to objects that don't meet function criteria
sapply(
ls(pattern="^results_"), # only objects that start with "results_"
function(x) if(nrow(x) == 0) x else NA # return names of objects of nrow length 0
)))