[R] Create and Assign value into a variable from Another variable

arun smartpink111 at yahoo.com
Tue Aug 28 18:50:03 CEST 2012


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



> > 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")))
> > 
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.





More information about the R-help mailing list