Skip to content
Prev 367148 / 398506 Next

[FORGED] Re: get() return nothing

Hi,

When you want to get 'something' out of a loop you need to assign that 'something' to a variable that persists outside of the loop. I think it is a scoping thing. In your situation you could create a list with as many elements as there are objects with 'txt' in their names.  I can't quite follow what is is you are after, but perhaps something like this (untested and I'm still on my first cup of coffee) ...

obj_names <- ls(pattern="txt")
obj_dims <- vector(mode = 'list', length = length(obj_names))
names(obj_dims) <- obj_names
for (nm in obj_names)){
    obj_dims[[nm]] <- dim(get(nm)) 
}

Does that do what you want?  If so, you could probably use lapply() for the purpose instead of the for loop, but even better is to store each of your objects in a list as you create them rather than letting them get loose in the global environment.  That way you don't have to do this get-by-name rodeo to get info on them.

Cheers,
Ben
Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org