[R] Faster way to do it??...using apply?

2010-12-28 Thread M.Ribeiro

Hi, 
I have a simple task, but I am looking for a clever and fast way to do it:

I have a vector x with 0,1 or 2 and I want to create another vector y with
the same length following the rules:
If the element in x is equal to 0, the element in y is equal to 0
If the element in x is equal to 2, the element in y is equal to 1
If the element in x is equal to 1, the element in y is either 0 or 1 (sample
from c(0,1))

thus the vector
 x
 [,1]
[1,]0
[2,]2
[3,]1
[4,]2
[5,]0
[6,]1
[7,]2

could produce the vector y (this is one of the possibilities since y|x=1 is
either 0 or 1

 y
 [,1]
[1,]0
[2,]1
[3,]1
[4,]1
[5,]0
[6,]0
[7,]1


I know how to do this using for loops but I was wondering if you guys could
suggest a better way
Thanks

-- 
View this message in context: 
http://r.789695.n4.nabble.com/Faster-way-to-do-it-using-apply-tp3166161p3166161.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] Faster way to do it??...using apply?

2010-12-28 Thread Henrique Dallazuanna
Try this:

replace(replace(x, x == 1, sample(0:1, 1)), x == 2, 1)

On Tue, Dec 28, 2010 at 2:43 PM, M.Ribeiro mresende...@yahoo.com.br wrote:


 Hi,
 I have a simple task, but I am looking for a clever and fast way to do it:

 I have a vector x with 0,1 or 2 and I want to create another vector y with
 the same length following the rules:
 If the element in x is equal to 0, the element in y is equal to 0
 If the element in x is equal to 2, the element in y is equal to 1
 If the element in x is equal to 1, the element in y is either 0 or 1
 (sample
 from c(0,1))

 thus the vector
  x
 [,1]
 [1,]0
 [2,]2
 [3,]1
 [4,]2
 [5,]0
 [6,]1
 [7,]2

 could produce the vector y (this is one of the possibilities since y|x=1 is
 either 0 or 1

  y
 [,1]
 [1,]0
 [2,]1
 [3,]1
 [4,]1
 [5,]0
 [6,]0
 [7,]1


 I know how to do this using for loops but I was wondering if you guys could
 suggest a better way
 Thanks

 --
 View this message in context:
 http://r.789695.n4.nabble.com/Faster-way-to-do-it-using-apply-tp3166161p3166161.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.




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

[[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] Faster way to do it??...using apply?

2010-12-28 Thread M.Ribeiro

Hi Henrique,
Thanks for the fast answer,
The only problem in your code, which I think I didn't mention in my message
is that I would like one different random sampling procedure for each 1 in
my vector

The way it was written, it samples only once and replace by every 1:
 x = as.matrix(c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1))
 replace(replace(x, x == 1, sample(0:1, 1)), x == 2, 1)
  [,1]
 [1,]1
 [2,]1
 [3,]1
 [4,]1
 [5,]1
 [6,]1
 [7,]1
 [8,]1
 [9,]1
[10,]1
[11,]1
[12,]1
[13,]1
[14,]1
[15,]1

Thanks

-- 
View this message in context: 
http://r.789695.n4.nabble.com/Faster-way-to-do-it-using-apply-tp3166161p3166203.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] Faster way to do it??...using apply?

2010-12-28 Thread Henrique Dallazuanna
Try this indeed

replace(replace(x, x == 1, sample(0:1, sum(x == 1), rep = TRUE)), x == 2, 1)

On Tue, Dec 28, 2010 at 3:14 PM, M.Ribeiro mresende...@yahoo.com.br wrote:


 Hi Henrique,
 Thanks for the fast answer,
 The only problem in your code, which I think I didn't mention in my message
 is that I would like one different random sampling procedure for each 1 in
 my vector

 The way it was written, it samples only once and replace by every 1:
  x = as.matrix(c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1))
  replace(replace(x, x == 1, sample(0:1, 1)), x == 2, 1)
   [,1]
  [1,]1
  [2,]1
  [3,]1
  [4,]1
  [5,]1
  [6,]1
  [7,]1
  [8,]1
  [9,]1
 [10,]1
 [11,]1
 [12,]1
 [13,]1
 [14,]1
 [15,]1

 Thanks

 --
 View this message in context:
 http://r.789695.n4.nabble.com/Faster-way-to-do-it-using-apply-tp3166161p3166203.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.




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

[[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] Faster way to do it??...using apply?

2010-12-28 Thread Jonathan P Daily
I don't know if it's any faster, but it is also possible this way:

y - ifelse(x ==1, round(runif(x)), sign(x))
--
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 12/28/2010 12:48:04 PM:

 [image removed] 
 
 Re: [R] Faster way to do it??...using apply?
 
 Henrique Dallazuanna 
 
 to:
 
 M.Ribeiro
 
 12/28/2010 12:51 PM
 
 Sent by:
 
 r-help-boun...@r-project.org
 
 Cc:
 
 r-help
 
 Try this indeed
 
 replace(replace(x, x == 1, sample(0:1, sum(x == 1), rep = TRUE)), x == 
2, 1)
 
 On Tue, Dec 28, 2010 at 3:14 PM, M.Ribeiro mresende...@yahoo.com.br 
wrote:
 
 
  Hi Henrique,
  Thanks for the fast answer,
  The only problem in your code, which I think I didn't mention in my 
message
  is that I would like one different random sampling procedure for each 
1 in
  my vector
 
  The way it was written, it samples only once and replace by every 1:
   x = as.matrix(c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1))
   replace(replace(x, x == 1, sample(0:1, 1)), x == 2, 1)
[,1]
   [1,]1
   [2,]1
   [3,]1
   [4,]1
   [5,]1
   [6,]1
   [7,]1
   [8,]1
   [9,]1
  [10,]1
  [11,]1
  [12,]1
  [13,]1
  [14,]1
  [15,]1
 
  Thanks
 
  --
  View this message in context:
  http://r.789695.n4.nabble.com/Faster-way-to-do-it-using-apply-
 tp3166161p3166203.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.
 
 
 
 
 -- 
 Henrique Dallazuanna
 Curitiba-Paraná-Brasil
 25° 25' 40 S 49° 16' 22 O
 
[[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.