Re: [R] help with creating new variables using a loop

2013-02-09 Thread Uwe Ligges



On 07.02.2013 17:16, christel lacaze wrote:


Hi there,

I've got a set of 10 numeric variables called Mood1 to Mood10 in a dataset 
called mood.  I'm trying to create a set of 10 new variables called m1 to m10 
so that m1=Mood1*1, m2=Mood2*2, etc to m10=Mood10*10

Trawling through the internet, I eventually tried the following code:

for (i in 1:10){
   assign(x=paste0(mood$m,i),
  value=paste0(mood$Mood,i)*i,
  envir=.GlobalEnv)
}




Indexing works via

mood[[paste0(Mood, i)]]


However, assignment to separate objects is typically a bad idea, just 
aollect them in another data.frame or list.


Uwe Ligges








unfortunately this doesn't work as it seems the paste0 function returns a list 
object that only contains the vector's name and not the vector itself.

Hope someone can help...

Christel

[[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] help with creating new variables using a loop

2013-02-09 Thread arun
Hi,
 mood- data.frame(Mood1=1:5,Mood2=6:10,Mood3=11:15)
lapply(seq_along(mood),function(i) i*mood[i])
A.K.




- Original Message -
From: christel lacaze christellac...@hotmail.co.uk
To: r-help@r-project.org
Cc: 
Sent: Thursday, February 7, 2013 11:16 AM
Subject: [R] help with creating new variables using a loop


Hi there,

I've got a set of 10 numeric variables called Mood1 to Mood10 in a dataset 
called mood.  I'm trying to create a set of 10 new variables called m1 to m10 
so that m1=Mood1*1, m2=Mood2*2, etc to m10=Mood10*10

Trawling through the internet, I eventually tried the following code:

for (i in 1:10){
  assign(x=paste0(mood$m,i),
         value=paste0(mood$Mood,i)*i,
         envir=.GlobalEnv)
}

unfortunately this doesn't work as it seems the paste0 function returns a list 
object that only contains the vector's name and not the vector itself.

Hope someone can help...

Christel
                          
    [[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] help with creating new variables using a loop

2013-02-09 Thread arun
Hi,

Just to add:
If the columns are  not in the order (Mood1:Mood10) in the dataset. 
mood- data.frame(Mood2=6:10,Mood1=1:5,Mood10=11:15,Mood3=16:20) 
do.call(cbind,lapply(names(mood),function(i) 
mood[i]*as.numeric(gsub(\\D+,,i

#  Mood2 Mood1 Mood10 Mood3
#1    12     1    110    48
#2    14     2    120    51
#3    16     3    130    54
#4    18     4    140    57
#5    20     5    150    60
A.K.


- Original Message -
From: christel lacaze christellac...@hotmail.co.uk
To: r-help@r-project.org
Cc: 
Sent: Thursday, February 7, 2013 11:16 AM
Subject: [R] help with creating new variables using a loop


Hi there,

I've got a set of 10 numeric variables called Mood1 to Mood10 in a dataset 
called mood.  I'm trying to create a set of 10 new variables called m1 to m10 
so that m1=Mood1*1, m2=Mood2*2, etc to m10=Mood10*10

Trawling through the internet, I eventually tried the following code:

for (i in 1:10){
  assign(x=paste0(mood$m,i),
         value=paste0(mood$Mood,i)*i,
         envir=.GlobalEnv)
}

unfortunately this doesn't work as it seems the paste0 function returns a list 
object that only contains the vector's name and not the vector itself.

Hope someone can help...

Christel
                          
    [[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] help with creating new variables using a loop

2013-02-07 Thread Barry Rowlingson
On Thu, Feb 7, 2013 at 4:16 PM, christel lacaze
christellac...@hotmail.co.uk wrote:

 Hi there,

 I've got a set of 10 numeric variables called Mood1 to Mood10 in a dataset 
 called mood.

 That's where you went wrong in the first place. Don't use variable
names for indexing purposes.

 You should have created one variable, a list, where each element is
one of your Mood1, Mood2 etc variables. You can do that:

Mood = list(Mood1,Mood2,Mood3 [etc])

 but its easier to create the right thing in the first place.

 Then looping is simple, and intuitive. Just get Mood2[[i]] where i is
your loop variable. No mucking with paste and get and all that stuff
we should have left behind years ago...

  I'm trying to create a set of 10 new variables called m1 to m10 so that 
 m1=Mood1*1, m2=Mood2*2, etc to m10=Mood10*10

 What you mean is you want to create a new variable, a list, with 10 elements.

 m = list()
 for(i in 1:10){m[[i]]=i*Mood[[i]]}

or even:

 m = lapply(1:10,function(i){i*Mood[[i]]})

this kind of thing is so much easier once you've put everything in an
indexable list, I'm not going to tell you how to paste names together.
It can be done, but... yuck.

Barry

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