Re: [R] How to use the paste function to create an already used variable

2010-03-25 Thread Sharpie


"Biedermann, Jürgen" wrote:
> 
> Perfekt!
> 
> So, the code below does, what I want it does.
> 
> for (m in 1:AnzRat) {
> Daten_akt <- eval(parse(text=paste("Daten",m,"_akt",sep="")))
> ...
> ...
> assign(paste("Daten",m,"_akt",sep=""),Daten_akt)
> }
> 

You may want to consider replacing eval(parse()) with a call to get(). 
eval(parse()) does the job- but it's like using a sledgehammer when you have
a tack hammer available that will work just fine.

-Charlie

-
Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University
-- 
View this message in context: 
http://n4.nabble.com/How-to-use-the-paste-function-to-create-an-already-used-variable-tp1680508p1690769.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] How to use the paste function to create an already used variable

2010-03-25 Thread Biedermann, Jürgen
Perfekt!

So, the code below does, what I want it does.

for (m in 1:AnzRat) {
Daten_akt <- eval(parse(text=paste("Daten",m,"_akt",sep="")))
...
...
assign(paste("Daten",m,"_akt",sep=""),Daten_akt)
}


Patrick Burns schrieb:
> I think you are looking for 'assign'.
>
> On 25/03/2010 09:40, "Biedermann, Jürgen" wrote:
>   
>> Thank you very much for the solutions! :-)
>>
>> It's my fault, but I forgot to mention that at the end of the loop, I
>> would like to give
>>
>> "Data" back to data1, data2, data3, data4
>>
>> The eval formula only seems to work in one direction (it's the same with
>> the get function)
>>
>> for (i in 1:4) {
>>  Data[[i]]<- eval(parse(text =  paste("data",i,sep="")))
>>  ...
>>  ...
>>  
>>  eval(parse(text =  paste("data",i,sep="")))<- Data[[i]] # -->  I get an 
>> error message here
>>
>>
>> }
>>
>> I know that generally it's much better to stick to lists in this kind of 
>> kontext,
>> but I don't wanna change the whole structure of the rest of my code.
>>
>> Thanks
>>
>> Greetings
>> Jürgen
>>
>>
>>
>>
>>
>>
>> David Winsemius schrieb:
>> 
>>> On Mar 24, 2010, at 9:25 AM, Tal Galili wrote:
>>>
>>>
>>>   
 Try:

 for (i in 1:4) {
 Data<- eval(parse(text =  paste("data",i,sep="")))
 ...
 ..
 }


 (and if there are other solutions - I would love to know)

 
>>> Well, it's not actually a solution since the first three fetched
>>> values would have been overwritten.
>>>
>>> Instead:
>>>
>>>Data<- list()
>>>for (i in 1:4) {
>>>  Data[[i]]<- eval(parse(text =  paste("data",i,sep=""))) }
>>>
>>>   
 Contact
 Details:---
 Contact me: tal.gal...@gmail.com |  972-52-7275845
 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il
 (Hebrew) |
 www.r-statistics.com (English)
 --




 2010/3/24 "Biedermann, Jürgen"


 
> Hi there,
>
> I have the following problem
>
> Four data frames exist:
>
> data1
> data2
> data3
> data4
>
> Now I want to write a loop and temporarily store the data1, data2,
> data3,
> data4 in a variable called data.
> I tried the following...
>
> for (i in 1:4) {
> Data<- paste("data",i,sep="")
> ...
> ..
> }
>
>
> but it doesn't function. I think the problem is the definition of
> the mode
> of the pasted variable.
>
> Could anyone help me?
> Thanks
>
> Greetings
> Jürgen
>
> __
> R-help@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.
>
>
>   
[[alternative HTML version deleted]]

 __
 R-help@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.

 
>>> David Winsemius, MD
>>> West Hartford, CT
>>>
>>>
>>>   
>>  [[alternative HTML version deleted]]
>>
>>
>>
>>
>> __
>> R-help@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.
>> 
>
>   


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] How to use the paste function to create an already used variable

2010-03-25 Thread Biedermann, Jürgen
Thank you very much for the solutions! :-)

It's my fault, but I forgot to mention that at the end of the loop, I 
would like to give

"Data" back to data1, data2, data3, data4

The eval formula only seems to work in one direction (it's the same with 
the get function)

for (i in 1:4) {
Data[[i]] <- eval(parse(text =  paste("data",i,sep=""))) 
...
...

eval(parse(text =  paste("data",i,sep=""))) <- Data[[i]] # --> I get an 
error message here


}

I know that generally it's much better to stick to lists in this kind of 
kontext,
but I don't wanna change the whole structure of the rest of my code.

Thanks

Greetings
Jürgen






David Winsemius schrieb:
> On Mar 24, 2010, at 9:25 AM, Tal Galili wrote:
>
>   
>> Try:
>>
>> for (i in 1:4) {
>> Data <- eval(parse(text =  paste("data",i,sep="")))
>> ...
>> ..
>> }
>>
>>
>> (and if there are other solutions - I would love to know)
>> 
>
> Well, it's not actually a solution since the first three fetched  
> values would have been overwritten.
>
> Instead:
>
>   Data <- list()
>   for (i in 1:4) {
> Data[[i]] <- eval(parse(text =  paste("data",i,sep=""))) }
>   
>>
>> Contact
>> Details:---
>> Contact me: tal.gal...@gmail.com |  972-52-7275845
>> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il  
>> (Hebrew) |
>> www.r-statistics.com (English)
>> --
>>
>>
>>
>>
>> 2010/3/24 "Biedermann, Jürgen" 
>>
>> 
>>> Hi there,
>>>
>>> I have the following problem
>>>
>>> Four data frames exist:
>>>
>>> data1
>>> data2
>>> data3
>>> data4
>>>
>>> Now I want to write a loop and temporarily store the data1, data2,  
>>> data3,
>>> data4 in a variable called data.
>>> I tried the following...
>>>
>>> for (i in 1:4) {
>>> Data <- paste("data",i,sep="")
>>> ...
>>> ..
>>> }
>>>
>>>
>>> but it doesn't function. I think the problem is the definition of  
>>> the mode
>>> of the pasted variable.
>>>
>>> Could anyone help me?
>>> Thanks
>>>
>>> Greetings
>>> Jürgen
>>>
>>> __
>>> R-help@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.
>>>
>>>   
>>  [[alternative HTML version deleted]]
>>
>> __
>> R-help@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.
>> 
>
> David Winsemius, MD
> West Hartford, CT
>
>   


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] How to use the paste function to create an already used variable

2010-03-24 Thread Henrique Dallazuanna
Try this:

Data <- lapply(ls(pattern = 'data[0-9]'), get)

Data[[1]] is data1
Data[[2]] is data2

etc.

2010/3/24 "Biedermann, Jürgen" :
> Hi there,
>
> I have the following problem
>
> Four data frames exist:
>
> data1
> data2
> data3
> data4
>
> Now I want to write a loop and temporarily store the data1, data2, data3,
> data4 in a variable called data.
> I tried the following...
>
> for (i in 1:4) {
> Data <- paste("data",i,sep="")
> ...
> ..
> }
>
>
> but it doesn't function. I think the problem is the definition of the mode
> of the pasted variable.
>
> Could anyone help me?
> Thanks
>
> Greetings
> Jürgen
>
> __
> R-help@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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

__
R-help@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.


Re: [R] How to use the paste function to create an already used variable

2010-03-24 Thread Duncan Murdoch

On 24/03/2010 9:25 AM, Tal Galili wrote:

Try:

for (i in 1:4) {
Data <- eval(parse(text =  paste("data",i,sep="")))
...
..
}


(and if there are other solutions - I would love to know)
  


The get() function is simpler if you're just retrieving a variable:

Data <- get(paste("data", i, sep=""))

Duncan Murdoch



Contact
Details:---
Contact me: tal.gal...@gmail.com |  972-52-7275845
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
--




2010/3/24 "Biedermann, Jürgen" 

> Hi there,
>
> I have the following problem
>
> Four data frames exist:
>
> data1
> data2
> data3
> data4
>
> Now I want to write a loop and temporarily store the data1, data2, data3,
> data4 in a variable called data.
> I tried the following...
>
> for (i in 1:4) {
> Data <- paste("data",i,sep="")
> ...
> ..
> }
>
>
> but it doesn't function. I think the problem is the definition of the mode
> of the pasted variable.
>
> Could anyone help me?
> Thanks
>
> Greetings
> Jürgen
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

  



__
R-help@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.



__
R-help@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.


Re: [R] How to use the paste function to create an already used variable

2010-03-24 Thread David Winsemius


On Mar 24, 2010, at 9:25 AM, Tal Galili wrote:


Try:

for (i in 1:4) {
Data <- eval(parse(text =  paste("data",i,sep="")))
...
..
}


(and if there are other solutions - I would love to know)


Well, it's not actually a solution since the first three fetched  
values would have been overwritten.


Instead:

 Data <- list()
 for (i in 1:4) {
   Data[[i]] <- eval(parse(text =  paste("data",i,sep=""))) }




Contact
Details:---
Contact me: tal.gal...@gmail.com |  972-52-7275845
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il  
(Hebrew) |

www.r-statistics.com (English)
--




2010/3/24 "Biedermann, Jürgen" 


Hi there,

I have the following problem

Four data frames exist:

data1
data2
data3
data4

Now I want to write a loop and temporarily store the data1, data2,  
data3,

data4 in a variable called data.
I tried the following...

for (i in 1:4) {
Data <- paste("data",i,sep="")
...
..
}


but it doesn't function. I think the problem is the definition of  
the mode

of the pasted variable.

Could anyone help me?
Thanks

Greetings
Jürgen

__
R-help@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.



[[alternative HTML version deleted]]

__
R-help@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.


David Winsemius, MD
West Hartford, CT

__
R-help@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.


Re: [R] How to use the paste function to create an already used variable

2010-03-24 Thread Tal Galili
Try:

for (i in 1:4) {
Data <- eval(parse(text =  paste("data",i,sep="")))
...
..
}


(and if there are other solutions - I would love to know)



Contact
Details:---
Contact me: tal.gal...@gmail.com |  972-52-7275845
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
--




2010/3/24 "Biedermann, Jürgen" 

> Hi there,
>
> I have the following problem
>
> Four data frames exist:
>
> data1
> data2
> data3
> data4
>
> Now I want to write a loop and temporarily store the data1, data2, data3,
> data4 in a variable called data.
> I tried the following...
>
> for (i in 1:4) {
> Data <- paste("data",i,sep="")
> ...
> ..
> }
>
>
> but it doesn't function. I think the problem is the definition of the mode
> of the pasted variable.
>
> Could anyone help me?
> Thanks
>
> Greetings
> Jürgen
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.