Skip to content

get() return nothing

7 messages · Fix Ace, Duncan Murdoch, David Winsemius +2 more

#
Hello, there,
I wrote a loop to check the dimension of all the .txt dataframes:> ls()
?[1] "actualpca.table" "b4galnt2"??????? "b4galnt2.txt"??? "data"
?[5] "galnt4"????????? "galnt4.txt"????? "galnt5"????????? "galnt5.txt"
?[9] "galnt6"????????? "galnt6.txt"????? "glyco"?????????? "glyco.txt"
[13] "i"?????????????? "mtscaled"??????? "newsig.table"??? "nicepca"
[17] "pca"???????????? "sig.txt"???????? "st3gal3"???????? "st3gal3.txt"
[21] "st3gal5"???????? "st3gal5.txt"???? "st6gal1"???????? "st6gal1.txt"
If I check individual ones, they are ok:
[1] 8 3
could anyone help me to figure out why it did not work with a loop?
Thanks a lot!

Ace
#
On 11/02/2017 1:33 PM, Fix Ace via R-help wrote:
It's the difference between

for (i in 1:10) i

(which prints nothing) and

for (i in 1:10) print(i)

Duncan Murdoch
1 day later
#
Well, I am not trying to print anything. I just would like to get the dimension information for all the dataframes I created. Could you please help me to develop the script?
Thanks.
Ace
On Saturday, February 11, 2017 7:53 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:

        
On 11/02/2017 1:33 PM, Fix Ace via R-help wrote:
It's the difference between

for (i in 1:10) i

(which prints nothing) and

for (i in 1:10) print(i)

Duncan Murdoch
#
You should post R code that builds objects of similar structure as your use case. 
(
At the moment we don't know how these dataframes are assembled (in a list?) or having names with a structure that we need to get() or with an associated character vector with their names that are not R names.)
#
On 14/02/17 05:50, Fix Ace via R-help wrote:

            
Yes you *are* trying to print something.  You are trying to print the 
dimension information, i.e. dim(get(i))!!! For Pete's sake (a) *think* 
about what you are doing and (b) *try* example that Duncan suggested to you.

cheers,

Rolf Turner

  
    
#
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
4 days later
#
Thank you very much for the information! I will try it!
Ace
On Tuesday, February 14, 2017 8:48 AM, Ben Tupper <btupper at bigelow.org> wrote:
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