0

First of all I apologise if the question title is a bit generic but I wasn't really sure how to phrase it. The problem is as follows:

I have a large list of objects which are all titled in the following way: O1921, O1922,...,O2014. I want to put these objects into one vector titled "objects".

I have tried the following:

> lambda<-1921:2014

> objects<-Olambda (This returns "object `Olambda not found')

I then tried:

> objects<-O[lambda] (This returns "object `O' not found)

I have tried many other ways and I still get similar errors. Does any one know how to do this?

Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
UltraEasy
  • 3
  • 4

2 Answers2

1

If you already have a vector that looks like O1921, O1922, etc. then try this:

objects <- vector_of_values

It appears you don't already have that vector, because R is throwing an Error. It's telling you there is no object in the environment called Olambda. So, you'll have to make one!

lambda <- 1921:2014
Olambda <- paste("O", lambda, sep = "")
objects <- Olambda

You can simplify this further:

objects <- paste("O", lambda, sep = "")

Based on your comment, if you want to take the log of what is in your objects:

objects <- strsplit(objects, "O")
objects <- na.omit(as.numeric(unlist(objects))) 
log_objects <- log(objects)

That should give you something to incorporate into your code.

Raphael K
  • 1,771
  • 1
  • 11
  • 21
  • This is close to what I want but not quite it. I want to be able to use functions on each object. So for example: >log(objects[1]) should return the log of O1921( which is a datset by the way) – UltraEasy Apr 26 '16 at 16:57
  • You can't take a log of a string, so you'll have to strip the "O" and convert to integer. Updating answer.... – Raphael K Apr 26 '16 at 16:58
1

If you are trying to store objects into a "vector," this "vector" is typically called a list in R. Let's say your objects are regression results or plots, the list object will take them all. To store objects with the names you have, you can use the following:

myObjects <- mget(ls(pattern="^O[12]"))

as long as there are no other objects with similar names. Otherwise, you would have to refine the regular expression.

You can extract one of these objects from the list as follows:

myObjects[[1]]

Even better, myObjects is a named list so your can refer to the objects by their original names:

myObjects[["O1921"]]
lmo
  • 35,764
  • 9
  • 49
  • 57
  • This works! I thank thee greatly – UltraEasy Apr 26 '16 at 16:59
  • @Imo One more thing though. I don't quite understand the argument in >myObjects – UltraEasy Apr 26 '16 at 17:05
  • @UltraEasy The set of characters inside the double quotes, "^O[12]", are a regular expression. Regular expressions are amazing and everyone should learn to use them. There are lots of sites that discuss the meanings of these, including many an SO post. Here was where I started out: [regex](http://www.regular-expressions.info/quickstart.html). "^" is an anchor: "start at the beginning of the string;" "[]" is a character class: "match any of the characters inside;" "O" is a literal or fixed character: "match the letter O". – lmo Apr 26 '16 at 17:14