[R] create dummy variables by for loop

2011-02-24 Thread Changbin Du
HI, Dear R community,

I try to create 100 dummy variables like the following:

ack$id_1 - (ack$ID==1)*1
ack$id_2 - (ack$ID==2)*1
..
.
ack$id_100 - (ack$ID==100)*1


I used the following codes:

for(i in 1:100){
  ack$id_[i] - (ack$ID==i)*1
 }

But only one column is created, can anyone help me?

Thanks a lot!


-- 
Sincerely,
Changbin
--

[[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] create dummy variables by for loop

2011-02-24 Thread Jonathan P Daily
See inline below.
--
Jonathan P. Daily
Technician - USGS Leetown Science Center
11649 Leetown Road
Kearneysville WV, 25430
(304) 724-4480
Is the room still a room when its empty? Does the room,
 the thing itself have purpose? Or do we, what's the word... imbue it.
 - Jubal Early, Firefly

r-help-boun...@r-project.org wrote on 02/24/2011 01:23:54 PM:

 [image removed] 
 
 [R] create dummy variables by for loop
 
 Changbin Du 
 
 to:
 
 r-help@r-project.org
 
 02/24/2011 01:25 PM
 
 Sent by:
 
 r-help-boun...@r-project.org
 
 HI, Dear R community,
 
 I try to create 100 dummy variables like the following:
 
 ack$id_1 - (ack$ID==1)*1
 ack$id_2 - (ack$ID==2)*1
 ..
 .
 ack$id_100 - (ack$ID==100)*1
 
 
 I used the following codes:
 
 for(i in 1:100){
   ack$id_[i] - (ack$ID==i)*1
  }
This doesn't do what you think it does. when i = 1, it assigns the result 
of (ack$ID==1)*1 to the first element of ack$id_

If you want ack$id_i to be created, try:

ack[[paste(id, i, sep = _)]] - (ack$ID==i)*1


 
 But only one column is created, can anyone help me?
 
 Thanks a lot!
 
 
 -- 
 Sincerely,
 Changbin
 --
 
[[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] create dummy variables by for loop

2011-02-24 Thread Changbin Du
Thanks to all, appreciated!

On Thu, Feb 24, 2011 at 11:18 AM, Jonathan P Daily jda...@usgs.gov wrote:

 See inline below.
 --
 Jonathan P. Daily
 Technician - USGS Leetown Science Center
 11649 Leetown Road
 Kearneysville WV, 25430
 (304) 724-4480
 Is the room still a room when its empty? Does the room,
  the thing itself have purpose? Or do we, what's the word... imbue it.
 - Jubal Early, Firefly

 r-help-boun...@r-project.org wrote on 02/24/2011 01:23:54 PM:

  [image removed]
 
  [R] create dummy variables by for loop
 
  Changbin Du
 
  to:
 
  r-help@r-project.org
 
  02/24/2011 01:25 PM
 
  Sent by:
 
  r-help-boun...@r-project.org
 
  HI, Dear R community,
 
  I try to create 100 dummy variables like the following:
 
  ack$id_1 - (ack$ID==1)*1
  ack$id_2 - (ack$ID==2)*1
  ..
  .
  ack$id_100 - (ack$ID==100)*1
 
 
  I used the following codes:
 
  for(i in 1:100){
ack$id_[i] - (ack$ID==i)*1
   }
 This doesn't do what you think it does. when i = 1, it assigns the result
 of (ack$ID==1)*1 to the first element of ack$id_

 If you want ack$id_i to be created, try:

 ack[[paste(id, i, sep = _)]] - (ack$ID==i)*1


 
  But only one column is created, can anyone help me?
 
  Thanks a lot!
 
 
  --
  Sincerely,
  Changbin
  --
 
  [[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.htmlhttp://www.r-project.org/posting-guide.html
  and provide commented, minimal, self-contained, reproducible code.




-- 
Sincerely,
Changbin
--

Changbin Du
DOE Joint Genome Institute
Bldg 400 Rm 457
2800 Mitchell Dr
Walnut Creet, CA 94598
Phone: 925-927-2856

[[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] create dummy variables by for loop

2011-02-24 Thread David Winsemius


On Feb 24, 2011, at 1:23 PM, Changbin Du wrote:


HI, Dear R community,

I try to create 100 dummy variables like the following:

ack$id_1 - (ack$ID==1)*1
ack$id_2 - (ack$ID==2)*1
..
.
ack$id_100 - (ack$ID==100)*1


I used the following codes:

for(i in 1:100){
 ack$id_[i] - (ack$ID==i)*1


You are creating only one new column named ack$id_

Putting the [ operator with a numeric argument after does not name a  
new column.



}
But only one column is created, can anyone help me?


If I tell you how to do that I would only be hurting you. You should  
be using factors. The modeling functions should all be capable of  
accepting them and if you are writing your own then it seems only  
sensible that you follow their example.







--

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] create dummy variables by for loop

2011-02-24 Thread Changbin Du
Thanks, David!

On Thu, Feb 24, 2011 at 11:30 AM, David Winsemius dwinsem...@comcast.netwrote:


 On Feb 24, 2011, at 1:23 PM, Changbin Du wrote:

 HI, Dear R community,

 I try to create 100 dummy variables like the following:

 ack$id_1 - (ack$ID==1)*1
 ack$id_2 - (ack$ID==2)*1
 ..
 .
 ack$id_100 - (ack$ID==100)*1


 I used the following codes:

 for(i in 1:100){
 ack$id_[i] - (ack$ID==i)*1


 You are creating only one new column named ack$id_

 Putting the [ operator with a numeric argument after does not name a new
 column.


}
 But only one column is created, can anyone help me?


 If I tell you how to do that I would only be hurting you. You should be
 using factors. The modeling functions should all be capable of accepting
 them and if you are writing your own then it seems only sensible that you
 follow their example.




 --

 David Winsemius, MD
 West Hartford, CT




-- 
Sincerely,
Changbin
--

Changbin Du
DOE Joint Genome Institute
Bldg 400 Rm 457
2800 Mitchell Dr
Walnut Creet, CA 94598
Phone: 925-927-2856

[[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] create dummy variables by for loop

2011-02-24 Thread Dimitri Liakhovitski
If your ack$ID variable is numeric, you could first turn it into a factor:

myfactor = as.factor(ack$ID)

And then use model.matrix to create dummy variables:

mydummies = model.matrix(~myfactor)[, -1]

You'll get as many dummy variables as values you have in ack$ID -
minus 1 (for the reference level).
Dimitri

On Thu, Feb 24, 2011 at 2:30 PM, Changbin Du changb...@gmail.com wrote:
 Thanks to all, appreciated!

 On Thu, Feb 24, 2011 at 11:18 AM, Jonathan P Daily jda...@usgs.gov wrote:

 See inline below.
 --
 Jonathan P. Daily
 Technician - USGS Leetown Science Center
 11649 Leetown Road
 Kearneysville WV, 25430
 (304) 724-4480
 Is the room still a room when its empty? Does the room,
  the thing itself have purpose? Or do we, what's the word... imbue it.
     - Jubal Early, Firefly

 r-help-boun...@r-project.org wrote on 02/24/2011 01:23:54 PM:

  [image removed]
 
  [R] create dummy variables by for loop
 
  Changbin Du
 
  to:
 
  r-help@r-project.org
 
  02/24/2011 01:25 PM
 
  Sent by:
 
  r-help-boun...@r-project.org
 
  HI, Dear R community,
 
  I try to create 100 dummy variables like the following:
 
  ack$id_1 - (ack$ID==1)*1
  ack$id_2 - (ack$ID==2)*1
  ..
  .
  ack$id_100 - (ack$ID==100)*1
 
 
  I used the following codes:
 
  for(i in 1:100){
                ack$id_[i] - (ack$ID==i)*1
                       }
 This doesn't do what you think it does. when i = 1, it assigns the result
 of (ack$ID==1)*1 to the first element of ack$id_

 If you want ack$id_i to be created, try:

 ack[[paste(id, i, sep = _)]] - (ack$ID==i)*1


 
  But only one column is created, can anyone help me?
 
  Thanks a lot!
 
 
  --
  Sincerely,
  Changbin
  --
 
      [[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.htmlhttp://www.r-project.org/posting-guide.html
  and provide commented, minimal, self-contained, reproducible code.




 --
 Sincerely,
 Changbin
 --

 Changbin Du
 DOE Joint Genome Institute
 Bldg 400 Rm 457
 2800 Mitchell Dr
 Walnut Creet, CA 94598
 Phone: 925-927-2856

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




-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.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] create dummy variables by for loop

2011-02-24 Thread Changbin Du
Thanks, Dimitri! It is really cool!

On Thu, Feb 24, 2011 at 11:43 AM, Dimitri Liakhovitski 
dimitri.liakhovit...@gmail.com wrote:

 If your ack$ID variable is numeric, you could first turn it into a factor:

 myfactor = as.factor(ack$ID)

 And then use model.matrix to create dummy variables:

 mydummies = model.matrix(~myfactor)[, -1]

 You'll get as many dummy variables as values you have in ack$ID -
 minus 1 (for the reference level).
 Dimitri

 On Thu, Feb 24, 2011 at 2:30 PM, Changbin Du changb...@gmail.com wrote:
  Thanks to all, appreciated!
 
  On Thu, Feb 24, 2011 at 11:18 AM, Jonathan P Daily jda...@usgs.gov
 wrote:
 
  See inline below.
  --
  Jonathan P. Daily
  Technician - USGS Leetown Science Center
  11649 Leetown Road
  Kearneysville WV, 25430
  (304) 724-4480
  Is the room still a room when its empty? Does the room,
   the thing itself have purpose? Or do we, what's the word... imbue it.
  - Jubal Early, Firefly
 
  r-help-boun...@r-project.org wrote on 02/24/2011 01:23:54 PM:
 
   [image removed]
  
   [R] create dummy variables by for loop
  
   Changbin Du
  
   to:
  
   r-help@r-project.org
  
   02/24/2011 01:25 PM
  
   Sent by:
  
   r-help-boun...@r-project.org
  
   HI, Dear R community,
  
   I try to create 100 dummy variables like the following:
  
   ack$id_1 - (ack$ID==1)*1
   ack$id_2 - (ack$ID==2)*1
   ..
   .
   ack$id_100 - (ack$ID==100)*1
  
  
   I used the following codes:
  
   for(i in 1:100){
 ack$id_[i] - (ack$ID==i)*1
}
  This doesn't do what you think it does. when i = 1, it assigns the
 result
  of (ack$ID==1)*1 to the first element of ack$id_
 
  If you want ack$id_i to be created, try:
 
  ack[[paste(id, i, sep = _)]] - (ack$ID==i)*1
 
 
  
   But only one column is created, can anyone help me?
  
   Thanks a lot!
  
  
   --
   Sincerely,
   Changbin
   --
  
   [[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.htmlhttp://www.r-project.org/posting-guide.html
 http://www.r-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
 
 
 
 
  --
  Sincerely,
  Changbin
  --
 
  Changbin Du
  DOE Joint Genome Institute
  Bldg 400 Rm 457
  2800 Mitchell Dr
  Walnut Creet, CA 94598
  Phone: 925-927-2856
 
 [[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.htmlhttp://www.r-project.org/posting-guide.html
  and provide commented, minimal, self-contained, reproducible code.
 



 --
 Dimitri Liakhovitski
 Ninah Consulting
 www.ninah.com




-- 
Sincerely,
Changbin
--

Changbin Du
DOE Joint Genome Institute
Bldg 400 Rm 457
2800 Mitchell Dr
Walnut Creet, CA 94598
Phone: 925-927-2856

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