Re: [R] Trying to pass arrays as arguments to a function

2008-10-20 Thread Claudia Beleites
> I'd like to avoid looping through an array in order to change values in
> the array as it takes too long.
> I red from an earlier post it can be done by "do.call" but never got it
> to work. The Idea is to change the value of "y" according to values in
> "x". Wherever "x" holds the value 3, the corresponding value in "y"
> should be set to 1.
y [x == 3] <- 1


-- 
Claudia Beleites
Dipartimento dei Materiali e delle Risorse Naturali
Università degli Studi di Trieste
Via Alfonso Valerio 6/a
I-34127 Trieste

phone: +39 (0 40) 5 58-34 47
email: [EMAIL PROTECTED]

__
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] Trying to pass arrays as arguments to a function

2008-10-20 Thread Philipp Pagel

On Mon, Oct 20, 2008 at 03:07:45PM +0200, Kåre Edvardsen wrote:
> I'd like to avoid looping through an array in order to change values in
> the array as it takes too long.
> I red from an earlier post it can be done by "do.call" but never got it
> to work. The Idea is to change the value of "y" according to values in
> "x". Wherever "x" holds the value 3, the corresponding value in "y"
> should be set to 1. 

This should do what you want - no looping required:

y[x==3] <- 1

cu
Philipp

-- 
Dr. Philipp Pagel
Lehrstuhl für Genomorientierte Bioinformatik
Technische Universität München
Wissenschaftszentrum Weihenstephan
85350 Freising, Germany
http://mips.gsf.de/staff/pagel

__
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] Trying to pass arrays as arguments to a function

2008-10-20 Thread Kaom Te

y <- ifelse(x == 3, 1, y)

-Kaom

On Oct 20, 2008, at 6:07 AM, Kåre Edvardsen wrote:

I'd like to avoid looping through an array in order to change values  
in

the array as it takes too long.
I red from an earlier post it can be done by "do.call" but never got  
it

to work. The Idea is to change the value of "y" according to values in
"x". Wherever "x" holds the value 3, the corresponding value in "y"
should be set to 1.

So I tried the following giving an error message:

#
x <- c(1,2,3,2,2,3,1,1,3,3)
y <- c(0,0,1,1,0,0,1,0,0,1)

Change_y <- function() {

if (x == 3) {y <- 1}

}

do.call("Change_y", as.list(x,y))

Error in Change_y(1, 2, 3, 2, 2, 3, 1, 1, 3, 3) :
   unused argument(s) ( ...)

##

How should it be done?

Cheers,
Kare

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


[R] Trying to pass arrays as arguments to a function

2008-10-20 Thread Kåre Edvardsen
I'd like to avoid looping through an array in order to change values in
the array as it takes too long.
I red from an earlier post it can be done by "do.call" but never got it
to work. The Idea is to change the value of "y" according to values in
"x". Wherever "x" holds the value 3, the corresponding value in "y"
should be set to 1. 

So I tried the following giving an error message:

#
x <- c(1,2,3,2,2,3,1,1,3,3)
y <- c(0,0,1,1,0,0,1,0,0,1)

Change_y <- function() {

if (x == 3) {y <- 1}
  
}

do.call("Change_y", as.list(x,y))

Error in Change_y(1, 2, 3, 2, 2, 3, 1, 1, 3, 3) :
unused argument(s) ( ...)

##

How should it be done?

Cheers,
Kare

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