1

I have a variable a=0.01

I then create a matrix b<-matrix(data=NA,ncol=2,nrow=9)

I would like to rename this matrix by adding the value stored in a to its name.

The results should be b_0.01

1
  • 2
    What is your use case? I bet there is a better alternative to what you are trying to do now. Commented Aug 30, 2013 at 10:52

2 Answers 2

4

You can use assign to get this done:

a = 0.01
b = matrix(data=NA,ncol=2,nrow=9)
assign(sprintf('b_%s', a), b)
b_0.01

In general, I would avoid creating data objects like this. In stead, I would use list's to create, store and manipulate groups of objects.

Sign up to request clarification or add additional context in comments.

Comments

3

I bet there are more elegant ways to achieve what you need, but this seems to work:

assign(x = paste("b", a, sep = "_"), value = b)

Edit following @Roland's comment:

rm(b)

Please note that I address your question in a narrow sense. As pointed out by both @Roland and @Paul Hiemstra, there may be more general aspects of the work-flow that could be fruitful to consider as well.

2 Comments

But that is not renaming. You end up with two objects. I am not sure if a copy is made in memory immediately, but as soon as you modify the new object, you'll need twice the memory. At least include rm(b).
@Roland, thanks for your comment! I add rm. I entirely agree that this is not renaming per se - it was just my best alternative. I would be happy to see pure renaming solution. And yes, I support your comment to OP.

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.