DifferentFrogs
DifferentFrogs

Reputation: 3

How can I modify the variable named in a dataframe cell?

I'm trying to modify a "variable" variable; that is to say, I wish to modify only that variable whose name matches the text in a cell of a dataframe/matrix.

For example, if matrix1[1,1] == "Rupert", I want to perform an operation on the variable Rupert (say, Rupert <- Rupert + 1). But if matrix1[1,1] == "Paddington", I want to perform the operation on the Paddington variable instead.

I've discovered the assign() function which allows me create new variables whose name is that of the text in a matrix, but I haven't been able to figure out how to modify variables in a similar fashion.

Thanks for your attention,

Alistair

Upvotes: 0

Views: 54

Answers (1)

Frank
Frank

Reputation: 66819

Using your example:

var <- matrix1[1,1]
assign(var,get(var)+1)

The get function can be found in the "See also" section of help(assign).

Upvotes: 1

Related Questions