Re: [R] using variable from for loop in naming new variables

2010-09-17 Thread Ivan Calandra
  Hi Jim,

First, I think it's better if you reply to the list, other users might 
be interested and have better answers.

Second, as other replies showed you, using a list is actually way easier 
than creating new objects every time. Check especially the reply from 
Joshua Wiley, which gives you great examples and guidance.

Last, regarding rm(list=ls()), what's the point of creating new objects 
in your loop if you remove them right after?! Maybe you intend to do 
stuff on them first. Anyway, I don't use Emacs so not sure why you do it 
that way. Just my limited opinion.

HTH,
Ivan

Le 9/17/2010 12:35, Maas James Dr (MED) a écrit :

 Hi Ivan,

 Thanks for suggestions, was just hoping to do it all in one step, 
 creating a list and then filling it all seems rather complicated ... 
 will look up *apply.

 As for removing objects, I run R interactively from within Emacs (ESS) 
 and stuff hangs around for successive evaluations, not sure where it 
 came from so I clean it out every time ... is there a better way?

 Thanks

 Jim

 Message: 65

 Date: Thu, 16 Sep 2010 18:03:29 +0200

 From: Ivan Calandra ivan.calan...@uni-hamburg.de

 To: r-help@r-project.org

 Subject: Re: [R] using variable from for loop in naming new variables

 Message-ID: 4c923fd1.8000...@uni-hamburg.de

 Content-Type: text/plain

   Hi!

 assign() should do the job.

 But using a list, and filling each element iteratively (with a for loop

 or with *apply() ) might be better, up to you.

 Btw, why do you remove your objects?

 HTH,

 Ivan

 Le 9/16/2010 17:44, Maas James Dr (MED) a ?crit :

  Simple one here ... but can't get it to work ...

 

  for (i in 1:4){

   paste(stuff,[i]),sep=)- 3 + i

  }

 

  ls()

  rm(list=ls())

 

 

 

  I just want it to create 4 new variables called stuff1, stuff2, 
 stuff3, stuff4 with the corresponding assignments.  I realise that 
 there are more elegant functions but this is just a model of a bigger 
 situation.

 

  Thanks

 

  Jim

 

 ===

 Dr. Jim Maas

 Research Associate in Network Meta-Analysis

 School of Medicine, Health Policy and Practice

 CD Annex, Room 1.04

 University of East Anglia

 Norwich, UK

 NR4 7TJ

 +44 (0) 1603 591412


-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de

**
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php


[[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] using variable from for loop in naming new variables

2010-09-17 Thread Joshua Wiley
On Fri, Sep 17, 2010 at 4:46 AM, Ivan Calandra
ivan.calan...@uni-hamburg.de wrote:
  Hi Jim,

 First, I think it's better if you reply to the list, other users might
 be interested and have better answers.

 Second, as other replies showed you, using a list is actually way easier
 than creating new objects every time. Check especially the reply from
 Joshua Wiley, which gives you great examples and guidance.

 Last, regarding rm(list=ls()), what's the point of creating new objects
 in your loop if you remove them right after?! Maybe you intend to do
 stuff on them first. Anyway, I don't use Emacs so not sure why you do it
 that way. Just my limited opinion.

 HTH,
 Ivan

 Le 9/17/2010 12:35, Maas James Dr (MED) a écrit :

 Hi Ivan,

 Thanks for suggestions, was just hoping to do it all in one step,
 creating a list and then filling it all seems rather complicated ...
 will look up *apply.

I'd be happy to try to help you simplify things, but to offer much
advice we would probably need to know more about your end goal.  At
least in the simple example you gave, the one step version is:
as.list(3 + 1:4)
but I am guessing you are not actually interested in that ;-)


 As for removing objects, I run R interactively from within Emacs (ESS)
 and stuff hangs around for successive evaluations, not sure where it
 came from so I clean it out every time ... is there a better way?

That will be because of the .Rhistory file I would think.  My
suggestion would be to clean it out once and save (so that its empty),
and from then on whenever you exit just use q(no) or just type q()
and R should ask you whether you want to save the workspace image and
you can hit n.

The only way I remember how to setup Emacs to not save by default is
to start R using C-u M-x R and then passing the command line
argument --no-save, but honestly that is so lengthy it would just be
easier to clean it out each time.  I know there's an easier way but I
never remember it.

Josh


 Thanks

 Jim

 Message: 65

 Date: Thu, 16 Sep 2010 18:03:29 +0200

 From: Ivan Calandra ivan.calan...@uni-hamburg.de

 To: r-help@r-project.org

 Subject: Re: [R] using variable from for loop in naming new variables

 Message-ID: 4c923fd1.8000...@uni-hamburg.de

 Content-Type: text/plain

   Hi!

 assign() should do the job.

 But using a list, and filling each element iteratively (with a for loop

 or with *apply() ) might be better, up to you.

 Btw, why do you remove your objects?

 HTH,

 Ivan

 Le 9/16/2010 17:44, Maas James Dr (MED) a ?crit :

  Simple one here ... but can't get it to work ...

 

  for (i in 1:4){

       paste(stuff,[i]),sep=)- 3 + i

  }

 

  ls()

  rm(list=ls())

 

 

 

  I just want it to create 4 new variables called stuff1, stuff2,
 stuff3, stuff4 with the corresponding assignments.  I realise that
 there are more elegant functions but this is just a model of a bigger
 situation.

 

  Thanks

 

  Jim

 

 ===

 Dr. Jim Maas

 Research Associate in Network Meta-Analysis

 School of Medicine, Health Policy and Practice

 CD Annex, Room 1.04

 University of East Anglia

 Norwich, UK

 NR4 7TJ

 +44 (0) 1603 591412


 --
 Ivan CALANDRA
 PhD Student
 University of Hamburg
 Biozentrum Grindel und Zoologisches Museum
 Abt. Säugetiere
 Martin-Luther-King-Platz 3
 D-20146 Hamburg, GERMANY
 +49(0)40 42838 6231
 ivan.calan...@uni-hamburg.de

 **
 http://www.for771.uni-bonn.de
 http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php


        [[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.





-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.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.


[R] using variable from for loop in naming new variables

2010-09-16 Thread Maas James Dr (MED)
Simple one here ... but can't get it to work ...

for (i in 1:4){
paste(stuff,[i]),sep=) - 3 + i
}

ls()
rm(list=ls())



I just want it to create 4 new variables called stuff1, stuff2, stuff3, stuff4 
with the corresponding assignments.  I realise that there are more elegant 
functions but this is just a model of a bigger situation.

Thanks

Jim


===
Dr. Jim Maas
University of East Anglia


[[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] using variable from for loop in naming new variables

2010-09-16 Thread Ivan Calandra
  Hi!

assign() should do the job.
But using a list, and filling each element iteratively (with a for loop 
or with *apply() ) might be better, up to you.

Btw, why do you remove your objects?

HTH,
Ivan

Le 9/16/2010 17:44, Maas James Dr (MED) a écrit :
 Simple one here ... but can't get it to work ...

 for (i in 1:4){
  paste(stuff,[i]),sep=)- 3 + i
 }

 ls()
 rm(list=ls())



 I just want it to create 4 new variables called stuff1, stuff2, stuff3, 
 stuff4 with the corresponding assignments.  I realise that there are more 
 elegant functions but this is just a model of a bigger situation.

 Thanks

 Jim


 ===
 Dr. Jim Maas
 University of East Anglia


   [[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.


-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de

**
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php


[[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] using variable from for loop in naming new variables

2010-09-16 Thread David Winsemius


On Sep 16, 2010, at 11:44 AM, Maas James Dr (MED) wrote:


Simple one here ... but can't get it to work ...

for (i in 1:4){
   paste(stuff,[i]),sep=) - 3 + i
}

ls()
rm(list=ls())


 for (i in 1:4){
+paste(stuff,[i]),sep=) - 3 + i
Error: unexpected '[' in:
for (i in 1:4){
   paste(stuff,[

So the first error pointed you to a problem
 }
Error: unexpected '}' in }

Fixing that error points you to another:
 for (i in 1:4){
+paste(stuff, i),sep=) - 3 + i
Error: unexpected ',' in:
for (i in 1:4){
   paste(stuff, i),

And when the syntax gets unsnarled you find that you are trying not  
using the correct semantics for assignment:


 for (i in 1:4){
+paste(stuff,i,sep=) - 3 + i
+ }
Error in paste(stuff, i, sep = ) - 3 + i :
  target of assignment expands to non-language object

You are currently attempting to assign a numeric value to a  
character vector. (That is, once stop throwing an error because  
putting the square brackets around i ... and you lose the right- 
paren before the sep argument.)  Try looking at the help page for  
assign (and working more basic examples):


?assign.





I just want it to create 4 new variables called stuff1, stuff2,  
stuff3, stuff4 with the corresponding assignments.  I realise that  
there are more elegant functions but this is just a model of a  
bigger situation.


Thanks

Jim


===
Dr. Jim Maas
University of East Anglia


[[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] using variable from for loop in naming new variables

2010-09-16 Thread Greg Snow
This is FAQ 7.21.

Note that the most important part of the FAQ is the last few lines where it 
essentially says DON'T DO THIS.  If this is just a model for a bigger 
situation then definitely Don't Do This, as using assign and global variables 
will just create more and bigger future headaches.  Use another data structure 
like a list and all this becomes much easier.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.s...@imail.org
801.408.8111


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
 project.org] On Behalf Of Maas James Dr (MED)
 Sent: Thursday, September 16, 2010 9:45 AM
 To: r-help@r-project.org
 Subject: [R] using variable from for loop in naming new variables
 
 Simple one here ... but can't get it to work ...
 
 for (i in 1:4){
 paste(stuff,[i]),sep=) - 3 + i
 }
 
 ls()
 rm(list=ls())
 
 
 
 I just want it to create 4 new variables called stuff1, stuff2, stuff3,
 stuff4 with the corresponding assignments.  I realise that there are
 more elegant functions but this is just a model of a bigger situation.
 
 Thanks
 
 Jim
 
 
 ===
 Dr. Jim Maas
 University of East Anglia
 
 
   [[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] using variable from for loop in naming new variables

2010-09-16 Thread Joshua Wiley
Dear Jim,

Just to follow up some of the great suggestions with some examples:

#

# Initialize a list, length 4
lstuff - vector(mode = list, length = 4)
# store the results of your for loop in it
for (i in 1:4) {
  lstuff[[i]] - 3 + i
}

# The assign() way
for(i in 1:4) {
  assign(x = paste(stuff, i, sep = ''),
 value = 3 + i, envir = .GlobalEnv)
}

# Now lets see about accessing the data
stuff1
lstuff[[1]]
lstuff # see all elements of the list at once

# So far, so good, but since they are named similarly
# maybe I want to do a similar operation on all of them

# Using the list

lstuff - lapply(lstuff, '+', y = 3)

# Using the results of the for loop
# stuff1 - stuff1 + 3
# for every single one, or...

for(i in 1:4) {
  assign(x = paste(stuff, i, sep = ''),
 value = get(paste(stuff, i, sep ='')) + 3,
 envir = .GlobalEnv)
}

# looking again

stuff1
lstuff[[1]]

# if you are concerned because the real thing is  more complex

lstuff[[1]] - data.frame(A = 1:10, B = 11:20)
lstuff[[2]] - matrix(rnorm(10))
# you can store much more complex objects in the list
# so no need to worry if stuff is actually going to be
# data frames or tables or ...



Cheers,

Josh

On Thu, Sep 16, 2010 at 8:44 AM, Maas James Dr (MED) j.m...@uea.ac.uk wrote:
 Simple one here ... but can't get it to work ...

 for (i in 1:4){
    paste(stuff,[i]),sep=) - 3 + i
 }

 ls()
 rm(list=ls())



 I just want it to create 4 new variables called stuff1, stuff2, stuff3, 
 stuff4 with the corresponding assignments.  I realise that there are more 
 elegant functions but this is just a model of a bigger situation.

 Thanks

 Jim


 ===
 Dr. Jim Maas
 University of East Anglia


        [[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.




-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.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.