Skip to content

Create and Assign value into a variable from Another variable

11 messages · Rantony, Bert Gunter, Akkara, Antony (GE Energy, Non-GE) +6 more

#
Hi,

Here i have a variable named "MyVariable" containing particular name

For eg:-  
MyVariable <- "UnitNo"

Here my requirement is, i need to get "UnitNo" as another variable by
reading "MyVariable"

i need  "UnitNo" as variable name and need to assign some value into that.

i had tried with as.name(MyVariable ) and as.character(MyVariable) - Its
getting that value, but im unable to assign
something into that variable.

Could you please help me ?

- Thank you
Antony



--
View this message in context: http://r.789695.n4.nabble.com/Create-and-Assign-value-into-a-variable-from-Another-variable-tp4641515.html
Sent from the R help mailing list archive at Nabble.com.
#
Post a small reproducible example to clarify what you want to do.

-- Bert
On Mon, Aug 27, 2012 at 11:48 PM, Rantony <antony.akkara at ge.com> wrote:

  
    
#
Ok.
Here I have a variable called "Variable_1".

Variable_1 <- "MyDataFrame"

Here I want to create another variable, by assigning the value of  "Variable_1" .
So, it will come like,

Assign(Variable_1,data.frame(read.csv("c:\\Mydata.csv")))   --->[this was the 1st requirement, now I got the solution]

Now, " MyDataFrame " is a variable and containing some values in that.
And Now, the problem what is I need to do "rbind" into the variable " MyDataFrame ".

I tried to do,
rbind(as.character(Variable_1), data.frame(read.csv("c:\\My2ndData.csv")))

and I tried this too

rbind(as.name(Variable_1), data.frame(read.csv("c:\\My2ndData.csv")))

and I getting the error like

Error: " object of type 'symbol' is not subsettable "


Here I am getting stuck !


- Thanks
Antony


-----Original Message-----
From: Bert Gunter [mailto:gunter.berton at gene.com]
Sent: Tuesday, August 28, 2012 1:46 PM
To: Akkara, Antony (GE Energy, Non-GE)
Cc: r-help at r-project.org
Subject: Re: [R] Create and Assign value into a variable from Another variable

Post a small reproducible example to clarify what you want to do.

-- Bert
On Mon, Aug 27, 2012 at 11:48 PM, Rantony <antony.akkara at ge.com> wrote:

  
    
#
This?
[1] 1
On 28.08.2012, at 10:16, Bert Gunter wrote:

            
#
Hello,

Try the following, supposing that 1:5 is the value you want to assign.

?assign
MyVariable <- "UnitNo"
assign(MyVariable, 1:5)
UnitNo

Hope this helps,

Rui Barradas

Em 28-08-2012 07:48, Rantony escreveu:
#
Ok.
Here I have a variable called "Variable_1".

Variable_1 <- "MyDataFrame"

Here I want to create another variable, by assigning the value of
"Variable_1" .
So, it will come like,

Assign(Variable_1,data.frame(read.csv("c:\\Mydata.csv")))   --->[this
was the 1st requirement, now I got the solution]

Now, " MyDataFrame " is a variable and containing some values in that.
And Now, the problem what is I need to do "rbind" into the variable "
MyDataFrame ".

I tried to do,
rbind(as.character(Variable_1),
data.frame(read.csv("c:\\My2ndData.csv")))

Here I am getting stuck !


- Thanks
Antony

-----Original Message-----
From: Rui Barradas [mailto:ruipbarradas at sapo.pt] 
Sent: Tuesday, August 28, 2012 3:48 PM
To: Akkara, Antony (GE Energy, Non-GE)
Cc: r-help
Subject: Re: [R] Create and Assign value into a variable from Another
variable

Hello,

Try the following, supposing that 1:5 is the value you want to assign.

?assign
MyVariable <- "UnitNo"
assign(MyVariable, 1:5)
UnitNo

Hope this helps,

Rui Barradas

Em 28-08-2012 07:48, Rantony escreveu:
that.
#
I'm not quite sure what you expect as a result there..
On 28.08.2012, at 12:34, Akkara, Antony (GE Energy, Non-GE) wrote:

            
#
The above code will try to rbind the character string "MyDataFrame" (unnecessarily coerced to character because it already is a character string) with the result of a read.csv (unnecessariliy coerced to a data frame because read.csv returns a data frame). That seems unlikely to be a useful combination.

I suspect you probably need something _like_ 
	rbind(get(Variable_1),  #because one of the things you want to rbind 
                                                          # is your data frame with a name in variable_1, 
                                                          #and you need to get() the data frame, not its name
                read.csv("c:\\My2ndData.csv") )     #because read.csv returns a data frame already
                
However, this won't update your data frame called "MyDataFrame", it'll just print it to the command line.
If you want to store it somewhere, you'll need to either assign it to a variable normally (eg newframe <- ) or wrap the above in another ugly 
assign(Variable_1, ...)

Why you are doing this via 'assign' is hard to understand, though. It's very convoluted. Lists are almost always better for handling collections of things that have to have different variable names.

S

*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}
#
Others have mentioned assign and get, but generally when the answer to
a question is "assign" it means that you are asking the wrong question
(see fortune(236)).

This is actually FAQ 7.21, the most useful part of the answer in the
FAQ is the last few lines.

If you tell us more about what you are actually trying to accomplish
then we have a better chance of giving you useful advice that is less
likely to lead to future frustration.
On Tue, Aug 28, 2012 at 12:48 AM, Rantony <antony.akkara at ge.com> wrote:

  
    
#
HI,
Try this:
?Variable_1<-"MyDataFrame"
?set.seed(1)
? assign(Variable_1,sample(1:5,replace=TRUE))
?assign(Variable_1,rbind(get(Variable_1),1:5))
?get(Variable_1)
#???? [,1] [,2] [,3] [,4] [,5]
#[1,]??? 2??? 2??? 3??? 5??? 2
#[2,]??? 1??? 2??? 3??? 4??? 5
A.K.




----- Original Message -----
From: S Ellison <S.Ellison at lgcgroup.com>
To: Jessica Streicher <j.streicher at micromata.de>; "Akkara, Antony (GE Energy, Non-GE)" <Antony.Akkara at ge.com>
Cc: r-help <r-help at r-project.org>
Sent: Tuesday, August 28, 2012 11:57 AM
Subject: Re: [R] Create and Assign value into a variable from Another variable
The above code will try to rbind the character string "MyDataFrame" (unnecessarily coerced to character because it already is a character string) with the result of a read.csv (unnecessariliy coerced to a data frame because read.csv returns a data frame). That seems unlikely to be a useful combination.

I suspect you probably need something _like_ 
??? rbind(get(Variable_1),? #because one of the things you want to rbind 
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # is your data frame with a name in variable_1, 
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #and you need to get() the data frame, not its name
? ? ? ? ? ? ? ? read.csv("c:\\My2ndData.csv") )? ?  #because read.csv returns a data frame already
? ? ? ? ? ? ? ? 
However, this won't update your data frame called "MyDataFrame", it'll just print it to the command line.
If you want to store it somewhere, you'll need to either assign it to a variable normally (eg newframe <- ) or wrap the above in another ugly 
assign(Variable_1, ...)

Why you are doing this via 'assign' is hard to understand, though. It's very convoluted. Lists are almost always better for handling collections of things that have to have different variable names.

S

*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.