2

I'm trying to print out all the variables in my environment that are of a certain type (for example 'matrix). I tried

for(f in ls()) {if(is.matrix(f) print(f) }

Obviously, this doesn't work because ls() returns a list of strings. I tried other things, but I can't seem to convert the string f into the variable f. Does anyone know how to do that?

In a nutshell, if I have a string variable y that holds "x", and x is also a variable in my environment, I want to use y to access x. Of course, I may not know a priori what value y holds.

0

2 Answers 2

2

You might want get, to access the object

for(f in ls()) { if(is.matrix(get(f))) print(f) }
Sign up to request clarification or add additional context in comments.

Comments

2

You can use mget() to get the objects corresponding to a vector of variable names

for(f in mget(ls())) { if(is.matrix(f)) print(f) }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.