[R] apply, lapply and data.frame in R 2.5

2007-07-30 Thread jiho
Hello everyone,

A recent (in 2.5 I suspect) change in R is giving me trouble. I want  
to apply a function (tolower) to all the columns of a data.frame and  
get a data.frame in return.
Currently, on a data.frame, both apply (for arrays) and lapply (for  
lists) work, but each returns its native class (resp. matrix and list):

apply(mydat,2,tolower)  # gives a matrix
lapply(mydat,tolower)   # gives a list
and
sapply(mydat,tolower)   # gives a matrix

If I remember well, apply did not used to work on data.frames and  
lapply returned a data.frame when it was provided with one, with the  
same properties (columns classes etc). At least this is what my code  
written with R 2.4.* suggests.

The solution would be:
as.data.frame(apply(mydat,2,tolower))
or
as.data.frame(lapply(mydat,tolower))

But this does not keep columns attributes (all columns are  
reinterpreted, for example strings are converted to factors etc). For  
my particular use stringsAsFactors=FALSE does what I need, but I am  
wondering wether there is a more general solution to apply a function  
on all elements of a data.frame and get a similar data.frame in  
return. Indeed data.frames are probably the most common object in R  
and applying a function to each of its columns/variables appears to  
me as something one would want to do quite often.

Thank you in advance.

JiHO
---
http://jo.irisson.free.fr/

__
R-help@stat.math.ethz.ch 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] apply, lapply and data.frame in R 2.5

2007-07-30 Thread Prof Brian Ripley
On Mon, 30 Jul 2007, jiho wrote:

 Hello everyone,

 A recent (in 2.5 I suspect) change in R is giving me trouble. I want
 to apply a function (tolower) to all the columns of a data.frame and
 get a data.frame in return.
 Currently, on a data.frame, both apply (for arrays) and lapply (for
 lists) work, but each returns its native class (resp. matrix and list):

 apply(mydat,2,tolower)# gives a matrix
 lapply(mydat,tolower) # gives a list
 and
 sapply(mydat,tolower) # gives a matrix

which is exactly what R 2.0.0 did, so no recent(ish) change at all.

 If I remember well, apply did not used to work on data.frames and
 lapply returned a data.frame when it was provided with one, with the
 same properties (columns classes etc). At least this is what my code
 written with R 2.4.* suggests.

apply has coerced data frames for many years and lapply always returned a 
list.  The solution has always been

mydat[] - lapply(mydat,tolower)


 The solution would be:
 as.data.frame(apply(mydat,2,tolower))
 or
 as.data.frame(lapply(mydat,tolower))

 But this does not keep columns attributes (all columns are
 reinterpreted, for example strings are converted to factors etc). For
 my particular use stringsAsFactors=FALSE does what I need, but I am
 wondering wether there is a more general solution to apply a function
 on all elements of a data.frame and get a similar data.frame in
 return. Indeed data.frames are probably the most common object in R
 and applying a function to each of its columns/variables appears to
 me as something one would want to do quite often.

 Thank you in advance.

 JiHO
 ---
 http://jo.irisson.free.fr/

 __
 R-help@stat.math.ethz.ch 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.


-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@stat.math.ethz.ch 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] apply, lapply and data.frame in R 2.5

2007-07-30 Thread jiho

On 2007-July-30  , at 12:20 , Prof Brian Ripley wrote:
 On Mon, 30 Jul 2007, jiho wrote:
 A recent (in 2.5 I suspect) change in R is giving me trouble. I want
 to apply a function (tolower) to all the columns of a data.frame and
 get a data.frame in return.
 Currently, on a data.frame, both apply (for arrays) and lapply (for
 lists) work, but each returns its native class (resp. matrix and  
 list):

 apply(mydat,2,tolower)   # gives a matrix
 lapply(mydat,tolower)# gives a list
 and
 sapply(mydat,tolower)# gives a matrix

 which is exactly what R 2.0.0 did, so no recent(ish) change at all.

 If I remember well, apply did not used to work on data.frames and
 lapply returned a data.frame when it was provided with one, with the
 same properties (columns classes etc). At least this is what my code
 written with R 2.4.* suggests.

 apply has coerced data frames for many years and lapply always  
 returned a list.  The solution has always been

 mydat[] - lapply(mydat,tolower)

sorry about that, my previous code was misleading and indeed your  
code above does exactly what I need. I should have tested this a bit  
further before posting. I was just afraid to install two different R  
versions I guess.
thank you again.

JiHO
---
http://jo.irisson.free.fr/

__
R-help@stat.math.ethz.ch 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] apply incompatible dimensions error

2007-07-24 Thread Bernzweig, Bruce \(Consultant\)
Hi,

I've created the following two matrices (mat1 and mat2) and a function
(f) to calculate the correlations between the two on a row by row basis.

mat1 - matrix(sample(1:500,50), ncol = 5, 
dimnames=list(paste(row, 1:10, sep=), 
paste(col, 1:5, sep=)))

mat2 - matrix(sample(501:1000,50), ncol = 5, 
dimnames=list(paste(row, 1:10, sep=), 
paste(col, 1:5, sep=)))

f - function(x,y) cor(x,y)

When the matrices are squares (# rows = # columns) I have no problems.

However, when they are not (as in the example above with 5 columns and
10 rows), I get the following error:

 apply(mat1, 1, f, y=mat2)
Error in cor(x, y, na.method, method == kendall) : 
incompatible dimensions

Any help would be appreciated.  Thanks!

- Bruce



**
Please be aware that, notwithstanding the fact that the pers...{{dropped}}

__
R-help@stat.math.ethz.ch 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] apply incompatible dimensions error

2007-07-24 Thread Gabor Grothendieck
Your apply is trying to take the correlations of the rows of mat1 with the
columns of mat2 which, of course, does not work if they have different
numbers of columns. I think you mean to take the correlations of the columns
of mat1 with the columns of mat2.  For example, to take the correlations
of the 5 columns of mat1 with the first 4 columns of mat2 try:

 cor(mat1, mat2[,1:4])
col1   col2   col3   col4
col1 -0.34624254 -0.2669519 -0.2705077  0.2183249
col2 -0.26553255 -0.2687643 -0.0865895  0.1819025
col3  0.19474613 -0.2334986  0.1746522  0.2326915
col4  0.09328338  0.5117784  0.2413143 -0.3374916
col5  0.27519716  0.1605331 -0.4057137  0.3282105


On 7/24/07, Bernzweig, Bruce (Consultant) [EMAIL PROTECTED] wrote:
 Hi,

 I've created the following two matrices (mat1 and mat2) and a function
 (f) to calculate the correlations between the two on a row by row basis.

mat1 - matrix(sample(1:500,50), ncol = 5,
dimnames=list(paste(row, 1:10, sep=),
paste(col, 1:5, sep=)))

mat2 - matrix(sample(501:1000,50), ncol = 5,
dimnames=list(paste(row, 1:10, sep=),
paste(col, 1:5, sep=)))

f - function(x,y) cor(x,y)

 When the matrices are squares (# rows = # columns) I have no problems.

 However, when they are not (as in the example above with 5 columns and
 10 rows), I get the following error:

  apply(mat1, 1, f, y=mat2)
 Error in cor(x, y, na.method, method == kendall) :
incompatible dimensions

 Any help would be appreciated.  Thanks!

 - Bruce



 **
 Please be aware that, notwithstanding the fact that the pers...{{dropped}}

 __
 R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] apply incompatible dimensions error

2007-07-24 Thread Benilton Carvalho
are you positive that your function is doing what you expect it to do?

it looks like you want something like:

sapply(1:10, function(i) cor(mat1[i,], mat2[i,]))

b

On Jul 24, 2007, at 11:05 AM, Bernzweig, Bruce ((Consultant)) wrote:

 Hi,

 I've created the following two matrices (mat1 and mat2) and a function
 (f) to calculate the correlations between the two on a row by row  
 basis.

   mat1 - matrix(sample(1:500,50), ncol = 5,
   dimnames=list(paste(row, 1:10, sep=),
   paste(col, 1:5, sep=)))

   mat2 - matrix(sample(501:1000,50), ncol = 5,
   dimnames=list(paste(row, 1:10, sep=),
   paste(col, 1:5, sep=)))

   f - function(x,y) cor(x,y)

 When the matrices are squares (# rows = # columns) I have no problems.

 However, when they are not (as in the example above with 5 columns and
 10 rows), I get the following error:

 apply(mat1, 1, f, y=mat2)
 Error in cor(x, y, na.method, method == kendall) :
 incompatible dimensions

 Any help would be appreciated.  Thanks!

 - Bruce



 **
 Please be aware that, notwithstanding the fact that the pers... 
 {{dropped}}

 __
 R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] apply incompatible dimensions error

2007-07-24 Thread Bernzweig, Bruce \(Consultant\)
Thanks Gabor!

You state that my apply is taking rows of mat1 with columns of mat2.

Is this because I have the y=mat2 parameter?

 apply(mat1, 1, f, y=mat2)

Actually, what I would like is to run the correlations on a row against
row basis:  mat1 row1 x mat2 row1, etc.

Thanks again,

- Bruce


-Original Message-
From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, July 24, 2007 11:31 AM
To: Bernzweig, Bruce (Consultant)
Cc: r-help@stat.math.ethz.ch
Subject: Re: [R] apply  incompatible dimensions error

Your apply is trying to take the correlations of the rows of mat1 with
the
columns of mat2 which, of course, does not work if they have different
numbers of columns. I think you mean to take the correlations of the
columns
of mat1 with the columns of mat2.  For example, to take the correlations
of the 5 columns of mat1 with the first 4 columns of mat2 try:

 cor(mat1, mat2[,1:4])
col1   col2   col3   col4
col1 -0.34624254 -0.2669519 -0.2705077  0.2183249
col2 -0.26553255 -0.2687643 -0.0865895  0.1819025
col3  0.19474613 -0.2334986  0.1746522  0.2326915
col4  0.09328338  0.5117784  0.2413143 -0.3374916
col5  0.27519716  0.1605331 -0.4057137  0.3282105


On 7/24/07, Bernzweig, Bruce (Consultant) [EMAIL PROTECTED] wrote:
 Hi,

 I've created the following two matrices (mat1 and mat2) and a function
 (f) to calculate the correlations between the two on a row by row
basis.

mat1 - matrix(sample(1:500,50), ncol = 5,
dimnames=list(paste(row, 1:10, sep=),
paste(col, 1:5, sep=)))

mat2 - matrix(sample(501:1000,50), ncol = 5,
dimnames=list(paste(row, 1:10, sep=),
paste(col, 1:5, sep=)))

f - function(x,y) cor(x,y)

 When the matrices are squares (# rows = # columns) I have no problems.

 However, when they are not (as in the example above with 5 columns and
 10 rows), I get the following error:

  apply(mat1, 1, f, y=mat2)
 Error in cor(x, y, na.method, method == kendall) :
incompatible dimensions

 Any help would be appreciated.  Thanks!

 - Bruce



 **
 Please be aware that, notwithstanding the fact that the
pers...{{dropped}}

 __
 R-help@stat.math.ethz.ch 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.




**
Please be aware that, notwithstanding the fact that the pers...{{dropped}}

__
R-help@stat.math.ethz.ch 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] apply incompatible dimensions error

2007-07-24 Thread Gabor Grothendieck
Then try this:

cor(t(mat1), t(mat2))

Also note

1. the above implies that mat1 and mat2 must have the same
number of columns since if x and y are vectors cor(x,y) only makes
sense if they have the same length.

2. the usual convention is that variables are stored as columns
andt that rows correspond to cases so typically you would have
(in terms of your mat1 and mat2):

Mat1 - t(mat1)
Mat2 - t(mat2)

and then use Mat1 and Mat2, e.g. cor(Mat1, Mat2)



On 7/24/07, Bernzweig, Bruce (Consultant) [EMAIL PROTECTED] wrote:
 Thanks Gabor!

 You state that my apply is taking rows of mat1 with columns of mat2.

 Is this because I have the y=mat2 parameter?

  apply(mat1, 1, f, y=mat2)

 Actually, what I would like is to run the correlations on a row against
 row basis:  mat1 row1 x mat2 row1, etc.

 Thanks again,

 - Bruce


 -Original Message-
 From: Gabor Grothendieck [mailto:[EMAIL PROTECTED]
 Sent: Tuesday, July 24, 2007 11:31 AM
 To: Bernzweig, Bruce (Consultant)
 Cc: r-help@stat.math.ethz.ch
 Subject: Re: [R] apply  incompatible dimensions error

 Your apply is trying to take the correlations of the rows of mat1 with
 the
 columns of mat2 which, of course, does not work if they have different
 numbers of columns. I think you mean to take the correlations of the
 columns
 of mat1 with the columns of mat2.  For example, to take the correlations
 of the 5 columns of mat1 with the first 4 columns of mat2 try:

  cor(mat1, mat2[,1:4])
col1   col2   col3   col4
 col1 -0.34624254 -0.2669519 -0.2705077  0.2183249
 col2 -0.26553255 -0.2687643 -0.0865895  0.1819025
 col3  0.19474613 -0.2334986  0.1746522  0.2326915
 col4  0.09328338  0.5117784  0.2413143 -0.3374916
 col5  0.27519716  0.1605331 -0.4057137  0.3282105


 On 7/24/07, Bernzweig, Bruce (Consultant) [EMAIL PROTECTED] wrote:
  Hi,
 
  I've created the following two matrices (mat1 and mat2) and a function
  (f) to calculate the correlations between the two on a row by row
 basis.
 
 mat1 - matrix(sample(1:500,50), ncol = 5,
 dimnames=list(paste(row, 1:10, sep=),
 paste(col, 1:5, sep=)))
 
 mat2 - matrix(sample(501:1000,50), ncol = 5,
 dimnames=list(paste(row, 1:10, sep=),
 paste(col, 1:5, sep=)))
 
 f - function(x,y) cor(x,y)
 
  When the matrices are squares (# rows = # columns) I have no problems.
 
  However, when they are not (as in the example above with 5 columns and
  10 rows), I get the following error:
 
   apply(mat1, 1, f, y=mat2)
  Error in cor(x, y, na.method, method == kendall) :
 incompatible dimensions
 
  Any help would be appreciated.  Thanks!
 
  - Bruce
 
 
 
  **
  Please be aware that, notwithstanding the fact that the
 pers...{{dropped}}
 
  __
  R-help@stat.math.ethz.ch 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.
 



 **
 Please be aware that, notwithstanding the fact that the person sending
 this communication has an address in Bear Stearns' e-mail system, this
 person is not an employee, agent or representative of Bear Stearns.
 Accordingly, this person has no power or authority to represent, make
 any recommendation, solicitation, offer or statements or disclose
 information on behalf of or in any way bind Bear Stearns or any of its
 affiliates.
 **


__
R-help@stat.math.ethz.ch 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] apply incompatible dimensions error

2007-07-24 Thread Benilton Carvalho
that's garbor's suggestion then.
sorry for the misunderstanding. :-)
b

On Jul 24, 2007, at 11:35 AM, Bernzweig, Bruce ((Consultant)) wrote:

 Thanks Benilton,

 I know what I want to do, just not sure how to do it using R.  The  
 help
 documentation is not very clear.

 What I am trying to do is calculate correlations on a row against row
 basis:  mat1 row1 x mat2 row1, mat1 row1 x mat2 row2, ... mat1 row1 x
 mat2 row-n, mat1 row-n, mat2 row-n

 - Bruce

 -Original Message-
 From: Benilton Carvalho [mailto:[EMAIL PROTECTED]
 Sent: Tuesday, July 24, 2007 11:31 AM
 To: Bernzweig, Bruce (Consultant)
 Cc: r-help@stat.math.ethz.ch
 Subject: Re: [R] apply  incompatible dimensions error

 are you positive that your function is doing what you expect it to do?

 it looks like you want something like:

 sapply(1:10, function(i) cor(mat1[i,], mat2[i,]))

 b

 On Jul 24, 2007, at 11:05 AM, Bernzweig, Bruce ((Consultant)) wrote:

 Hi,

 I've created the following two matrices (mat1 and mat2) and a  
 function
 (f) to calculate the correlations between the two on a row by row
 basis.

  mat1 - matrix(sample(1:500,50), ncol = 5,
  dimnames=list(paste(row, 1:10, sep=),
  paste(col, 1:5, sep=)))

  mat2 - matrix(sample(501:1000,50), ncol = 5,
  dimnames=list(paste(row, 1:10, sep=),
  paste(col, 1:5, sep=)))

  f - function(x,y) cor(x,y)

 When the matrices are squares (# rows = # columns) I have no  
 problems.

 However, when they are not (as in the example above with 5 columns  
 and
 10 rows), I get the following error:

 apply(mat1, 1, f, y=mat2)
 Error in cor(x, y, na.method, method == kendall) :
 incompatible dimensions

 Any help would be appreciated.  Thanks!

 - Bruce



 * 
 *
 Please be aware that, notwithstanding the fact that the pers...
 {{dropped}}

 __
 R-help@stat.math.ethz.ch 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.



 **
 Please be aware that, notwithstanding the fact that the person sending
 this communication has an address in Bear Stearns' e-mail system, this
 person is not an employee, agent or representative of Bear Stearns.
 Accordingly, this person has no power or authority to represent, make
 any recommendation, solicitation, offer or statements or disclose
 information on behalf of or in any way bind Bear Stearns or any of its
 affiliates.
 **

__
R-help@stat.math.ethz.ch 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] apply incompatible dimensions error

2007-07-24 Thread Bernzweig, Bruce \(Consultant\)
Thanks for the explanation.

As for the rows/columns thing, the data I receive is given to me in that
way.  I currently read it in using read.csv.  Is there a function I
should look at that can take that and transpose it or should I just
process the data first outside of R?

Thanks,

- Bruce

-Original Message-
From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, July 24, 2007 11:43 AM
To: Bernzweig, Bruce (Consultant)
Cc: r-help@stat.math.ethz.ch
Subject: Re: [R] apply  incompatible dimensions error

Then try this:

cor(t(mat1), t(mat2))

Also note

1. the above implies that mat1 and mat2 must have the same
number of columns since if x and y are vectors cor(x,y) only makes
sense if they have the same length.

2. the usual convention is that variables are stored as columns
andt that rows correspond to cases so typically you would have
(in terms of your mat1 and mat2):

Mat1 - t(mat1)
Mat2 - t(mat2)

and then use Mat1 and Mat2, e.g. cor(Mat1, Mat2)



On 7/24/07, Bernzweig, Bruce (Consultant) [EMAIL PROTECTED] wrote:
 Thanks Gabor!

 You state that my apply is taking rows of mat1 with columns of mat2.

 Is this because I have the y=mat2 parameter?

  apply(mat1, 1, f, y=mat2)

 Actually, what I would like is to run the correlations on a row
against
 row basis:  mat1 row1 x mat2 row1, etc.

 Thanks again,

 - Bruce


 -Original Message-
 From: Gabor Grothendieck [mailto:[EMAIL PROTECTED]
 Sent: Tuesday, July 24, 2007 11:31 AM
 To: Bernzweig, Bruce (Consultant)
 Cc: r-help@stat.math.ethz.ch
 Subject: Re: [R] apply  incompatible dimensions error

 Your apply is trying to take the correlations of the rows of mat1 with
 the
 columns of mat2 which, of course, does not work if they have different
 numbers of columns. I think you mean to take the correlations of the
 columns
 of mat1 with the columns of mat2.  For example, to take the
correlations
 of the 5 columns of mat1 with the first 4 columns of mat2 try:

  cor(mat1, mat2[,1:4])
col1   col2   col3   col4
 col1 -0.34624254 -0.2669519 -0.2705077  0.2183249
 col2 -0.26553255 -0.2687643 -0.0865895  0.1819025
 col3  0.19474613 -0.2334986  0.1746522  0.2326915
 col4  0.09328338  0.5117784  0.2413143 -0.3374916
 col5  0.27519716  0.1605331 -0.4057137  0.3282105


 On 7/24/07, Bernzweig, Bruce (Consultant) [EMAIL PROTECTED] wrote:
  Hi,
 
  I've created the following two matrices (mat1 and mat2) and a
function
  (f) to calculate the correlations between the two on a row by row
 basis.
 
 mat1 - matrix(sample(1:500,50), ncol = 5,
 dimnames=list(paste(row, 1:10, sep=),
 paste(col, 1:5, sep=)))
 
 mat2 - matrix(sample(501:1000,50), ncol = 5,
 dimnames=list(paste(row, 1:10, sep=),
 paste(col, 1:5, sep=)))
 
 f - function(x,y) cor(x,y)
 
  When the matrices are squares (# rows = # columns) I have no
problems.
 
  However, when they are not (as in the example above with 5 columns
and
  10 rows), I get the following error:
 
   apply(mat1, 1, f, y=mat2)
  Error in cor(x, y, na.method, method == kendall) :
 incompatible dimensions
 
  Any help would be appreciated.  Thanks!
 
  - Bruce
 
 
 
 
**
  Please be aware that, notwithstanding the fact that the
 pers...{{dropped}}
 
  __
  R-help@stat.math.ethz.ch 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.
 



 **
 Please be aware that, notwithstanding the fact that the person sending
 this communication has an address in Bear Stearns' e-mail system, this
 person is not an employee, agent or representative of Bear Stearns.
 Accordingly, this person has no power or authority to represent, make
 any recommendation, solicitation, offer or statements or disclose
 information on behalf of or in any way bind Bear Stearns or any of its
 affiliates.
 **




**
Please be aware that, notwithstanding the fact that the pers...{{dropped}}

__
R-help@stat.math.ethz.ch 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] apply incompatible dimensions error

2007-07-24 Thread Bernzweig, Bruce \(Consultant\)
Thanks Benilton,

I know what I want to do, just not sure how to do it using R.  The help
documentation is not very clear.

What I am trying to do is calculate correlations on a row against row
basis:  mat1 row1 x mat2 row1, mat1 row1 x mat2 row2, ... mat1 row1 x
mat2 row-n, mat1 row-n, mat2 row-n

- Bruce

-Original Message-
From: Benilton Carvalho [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, July 24, 2007 11:31 AM
To: Bernzweig, Bruce (Consultant)
Cc: r-help@stat.math.ethz.ch
Subject: Re: [R] apply  incompatible dimensions error

are you positive that your function is doing what you expect it to do?

it looks like you want something like:

sapply(1:10, function(i) cor(mat1[i,], mat2[i,]))

b

On Jul 24, 2007, at 11:05 AM, Bernzweig, Bruce ((Consultant)) wrote:

 Hi,

 I've created the following two matrices (mat1 and mat2) and a function
 (f) to calculate the correlations between the two on a row by row  
 basis.

   mat1 - matrix(sample(1:500,50), ncol = 5,
   dimnames=list(paste(row, 1:10, sep=),
   paste(col, 1:5, sep=)))

   mat2 - matrix(sample(501:1000,50), ncol = 5,
   dimnames=list(paste(row, 1:10, sep=),
   paste(col, 1:5, sep=)))

   f - function(x,y) cor(x,y)

 When the matrices are squares (# rows = # columns) I have no problems.

 However, when they are not (as in the example above with 5 columns and
 10 rows), I get the following error:

 apply(mat1, 1, f, y=mat2)
 Error in cor(x, y, na.method, method == kendall) :
 incompatible dimensions

 Any help would be appreciated.  Thanks!

 - Bruce



 **
 Please be aware that, notwithstanding the fact that the pers... 
 {{dropped}}

 __
 R-help@stat.math.ethz.ch 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.



**
Please be aware that, notwithstanding the fact that the pers...{{dropped}}

__
R-help@stat.math.ethz.ch 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] apply a function to loop through

2007-07-17 Thread Hai Lin
Dear R users,

I have a dataset generated as follows,

myDat - data.frame(matrix(c(rep(LETTERS[1:3],
each=5),
 rnorm(5,mean=3,sd=0.03),
 rnorm(5,12,1),
 rnorm(5,1,0.5)),
   ncol=2,
  
dimnames=list(c(letters[1:15]),

c(tissue,Scores
myDat$Grp -c(gene1) 

There is one level gene1 in $Grp in my data step.
I'd like to do pairwise.wilcox.test on $tissue while
going throug $Grp if there are more levels with gene2,
gene3.

I tried to loop through $Grp using apply with an error
message Error in sort(unique.default(x), na.last =
TRUE) : 
'x' must be atomic. 

mytry -  apply(as.matrix(myDat),
1,
   
function(Grp)pairwise.wilcox.test(Grp$Scores,
  
Grp$tissue,
  
p.adjust.method = none)$p.value) 

I could not find any similar stuffs in forum. Could
anyone here give a hand?

Thanks a bunch.

kevin

__
R-help@stat.math.ethz.ch 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] apply a function to loop through

2007-07-17 Thread Weiwei Shi
Hi,

first of all, your Scores are factors instead of numeric, you need to change it;

I made a new myDat for test purpose by

myDat = as.data.frame(rbind(myDat, myDat))
myDat[16:30, 3] - gene2

 myDat
   tissueScores   Grp
a   A  3.01494535196933 gene1
b   A  2.99647624484379 gene1
c   A  3.00527533284709 gene1
d   A  3.02321059636917 gene1
e   A  3.04694197334289 gene1
f   B  11.1464974692841 gene1
g   B  12.2372838904939 gene1
h   B  11.7277801841221 gene1
i   B  11.6405683605147 gene1
j   B  11.1631961720026 gene1
k   C  0.67528704974662 gene1
l   C  1.21000950157251 gene1
m   C 0.843722400594721 gene1
n   C 0.881706314004343 gene1
o   C  1.43670211710054 gene1
a1  A  3.01494535196933 gene2
b1  A  2.99647624484379 gene2
c1  A  3.00527533284709 gene2
d1  A  3.02321059636917 gene2
e1  A  3.04694197334289 gene2
f1  B  11.1464974692841 gene2
g1  B  12.2372838904939 gene2
h1  B  11.7277801841221 gene2
i1  B  11.6405683605147 gene2
j1  B  11.1631961720026 gene2
k1  C  0.67528704974662 gene2
l1  C  1.21000950157251 gene2
m1  C 0.843722400594721 gene2
n1  C 0.881706314004343 gene2
o1  C  1.43670211710054 gene2
 mytry - by(myDat, INDICES=as.factor(myDat[,3]), FUN=function(x) {
+ pairwise.wilcox.test(as.numeric(as.character(x$Scores)),
+
+ x$tissue,
+
+ p.adjust.method = none)$p.value
+ })
 mytry
as.factor(myDat[, 3]): gene1
A   B
B 0.007936508  NA
C 0.007936508 0.007936508

as.factor(myDat[, 3]): gene2
A   B
B 0.007936508  NA
C 0.007936508 0.007936508


HTH,

Weiwei

On 7/17/07, Hai Lin [EMAIL PROTECTED] wrote:
 Dear R users,

 I have a dataset generated as follows,

 myDat - data.frame(matrix(c(rep(LETTERS[1:3],
 each=5),
  rnorm(5,mean=3,sd=0.03),
  rnorm(5,12,1),
  rnorm(5,1,0.5)),
ncol=2,

 dimnames=list(c(letters[1:15]),

 c(tissue,Scores
 myDat$Grp -c(gene1)

 There is one level gene1 in $Grp in my data step.
 I'd like to do pairwise.wilcox.test on $tissue while
 going throug $Grp if there are more levels with gene2,
 gene3.

 I tried to loop through $Grp using apply with an error
 message Error in sort(unique.default(x), na.last =
 TRUE) :
 'x' must be atomic.

 mytry -  apply(as.matrix(myDat),
 1,

 function(Grp)pairwise.wilcox.test(Grp$Scores,

 Grp$tissue,

 p.adjust.method = none)$p.value)

 I could not find any similar stuffs in forum. Could
 anyone here give a hand?

 Thanks a bunch.

 kevin

 __
 R-help@stat.math.ethz.ch 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.



-- 
Weiwei Shi, Ph.D
Research Scientist
GeneGO, Inc.

Did you always know?
No, I did not. But I believed...
---Matrix III

__
R-help@stat.math.ethz.ch 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] apply( )

2007-05-11 Thread Greg Tarpinian
Thank you all for your answers...  To summarize,

  1. with(X, ifelse(V1  V2 | V1  V3, 1, 0))
  2. ifelse((foo$x  foo$y) | (foo$x  foo$z), 1, 0)
  3. with(foo, (x  y) * (x  z))

were the responses to my question (see below).

Kindly,

Greg


--- Original post ---
 I have a question that must have a simple answer (but eludes me).
 I need a row-by-row logical comparison across three numeric variables
 in a data frame: foo$x, foo$y, foo$z.  The logic is
 
if( x  y || x  z ) 1 else 0
 
 for a particular row.
 
 It is simple and very inefficient to use for(i in 1:length(foo$x)){ }
 loops.  How can I accomplish this using sappy( ) / lapply( ) / 
 apply( ) or some other more efficient method?

 Thank you in advance,
 
 Greg

__
R-help@stat.math.ethz.ch 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] apply( )

2007-05-11 Thread Petr PIKAL
Hi
[EMAIL PROTECTED] napsal dne 10.05.2007 17:59:22:

 or
 
with(foo, (x  y) * (x  z))


Should not it be

with(foo, ((x  y) | (x  z))*1)

Regards
Petr


 
 On 5/10/07, jim holtman [EMAIL PROTECTED] wrote:
  You don't need apply.  Just do
 
  foo$result - ifelse((foo$x  foo$y) | (foo$x  foo$z), 1, 0)
 
  On 5/10/07, Greg Tarpinian [EMAIL PROTECTED] wrote:
   I have a question that must have a simple answer (but eludes me).
   I need a row-by-row logical comparison across three numeric 
variables
   in
   a data frame: foo$x, foo$y, foo$z.  The logic is
  
 if( x  y || x  z ) 1 else 0
  
   for a particular row.
  
   It is simple and very inefficient to use for(i in 1:length(foo$x)){ 
}
   loops.  How can I accomplish this using sappy( ) / lapply( ) / 
apply( )
   or some other more efficient method?
  
   Thank you in advance,
  
  Greg
  
   __
   R-help@stat.math.ethz.ch 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.
  
 
 
  --
  Jim Holtman
  Cincinnati, OH
  +1 513 646 9390
 
  What is the problem you are trying to solve?
 
  __
  R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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@stat.math.ethz.ch 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] apply( )

2007-05-10 Thread Greg Tarpinian
I have a question that must have a simple answer (but eludes me).
I need a row-by-row logical comparison across three numeric variables
in
a data frame: foo$x, foo$y, foo$z.  The logic is

   if( x  y || x  z ) 1 else 0

for a particular row.

It is simple and very inefficient to use for(i in 1:length(foo$x)){ }
loops.  How can I accomplish this using sappy( ) / lapply( ) / apply( )
or some other more efficient method?

Thank you in advance,

Greg

__
R-help@stat.math.ethz.ch 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] apply( )

2007-05-10 Thread jim holtman
You don't need apply.  Just do

foo$result - ifelse((foo$x  foo$y) | (foo$x  foo$z), 1, 0)

On 5/10/07, Greg Tarpinian [EMAIL PROTECTED] wrote:
 I have a question that must have a simple answer (but eludes me).
 I need a row-by-row logical comparison across three numeric variables
 in
 a data frame: foo$x, foo$y, foo$z.  The logic is

   if( x  y || x  z ) 1 else 0

 for a particular row.

 It is simple and very inefficient to use for(i in 1:length(foo$x)){ }
 loops.  How can I accomplish this using sappy( ) / lapply( ) / apply( )
 or some other more efficient method?

 Thank you in advance,

Greg

 __
 R-help@stat.math.ethz.ch 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.



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

__
R-help@stat.math.ethz.ch 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] apply( )

2007-05-10 Thread Chuck Cleland
Greg Tarpinian wrote:
 I have a question that must have a simple answer (but eludes me).
 I need a row-by-row logical comparison across three numeric variables
 in
 a data frame: foo$x, foo$y, foo$z.  The logic is
 
if( x  y || x  z ) 1 else 0
 
 for a particular row.
 
 It is simple and very inefficient to use for(i in 1:length(foo$x)){ }
 loops.  How can I accomplish this using sappy( ) / lapply( ) / apply( )
 or some other more efficient method?

X - as.data.frame(matrix(rnorm(30), ncol=3))
X
V1 V2 V3
1  -0.48026236  0.8629789 -1.2600858
2  -1.32408219 -0.5590268  1.1310638
3   0.02717575 -0.5661402  0.7824019
4   0.80783373  0.2300440 -0.4477275
5   1.24518907 -0.3778392  1.7546530
6  -0.39254125 -1.0388962 -0.4436296
7  -1.44473455  1.8606963  0.4253889
8  -0.63543047 -1.6408418 -1.0409473
9   0.81075970  0.3914066 -1.0361739
10  1.66021280 -1.6694101 -0.4810839

with(X, ifelse(V1  V2 | V1  V3, 1, 0))
 [1] 1 1 0 1 0 1 1 1 1 1

 Thank you in advance,
 
 Greg
 
 __
 R-help@stat.math.ethz.ch 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. 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894

__
R-help@stat.math.ethz.ch 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] apply( )

2007-05-10 Thread Gabor Grothendieck
or

   with(foo, (x  y) * (x  z))

On 5/10/07, jim holtman [EMAIL PROTECTED] wrote:
 You don't need apply.  Just do

 foo$result - ifelse((foo$x  foo$y) | (foo$x  foo$z), 1, 0)

 On 5/10/07, Greg Tarpinian [EMAIL PROTECTED] wrote:
  I have a question that must have a simple answer (but eludes me).
  I need a row-by-row logical comparison across three numeric variables
  in
  a data frame: foo$x, foo$y, foo$z.  The logic is
 
if( x  y || x  z ) 1 else 0
 
  for a particular row.
 
  It is simple and very inefficient to use for(i in 1:length(foo$x)){ }
  loops.  How can I accomplish this using sappy( ) / lapply( ) / apply( )
  or some other more efficient method?
 
  Thank you in advance,
 
 Greg
 
  __
  R-help@stat.math.ethz.ch 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.
 


 --
 Jim Holtman
 Cincinnati, OH
 +1 513 646 9390

 What is the problem you are trying to solve?

 __
 R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] apply( )

2007-05-10 Thread Petr Klasterecky
ifelse(((x  y) | (x  z)), 1, 0)

Note in particular the use of | instead of || for elementwise comparisons.

Petr

Greg Tarpinian napsal(a):
 I have a question that must have a simple answer (but eludes me).
 I need a row-by-row logical comparison across three numeric variables
 in
 a data frame: foo$x, foo$y, foo$z.  The logic is
 
if( x  y || x  z ) 1 else 0
 
 for a particular row.
 
 It is simple and very inefficient to use for(i in 1:length(foo$x)){ }
 loops.  How can I accomplish this using sappy( ) / lapply( ) / apply( )
 or some other more efficient method?
 
 Thank you in advance,
 
 Greg
 
 __
 R-help@stat.math.ethz.ch 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.
 

-- 
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic

__
R-help@stat.math.ethz.ch 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] apply( )

2007-05-10 Thread Gabor Grothendieck
Sorry, you wanted or, not and.

with(foo, pmax(x  y, x  z))

with(foo, as.numeric(x  y | x  z))

with(foo, 1*(x  y | x  z))





On 5/10/07, Gabor Grothendieck [EMAIL PROTECTED] wrote:
 or

   with(foo, (x  y) * (x  z))

 On 5/10/07, jim holtman [EMAIL PROTECTED] wrote:
  You don't need apply.  Just do
 
  foo$result - ifelse((foo$x  foo$y) | (foo$x  foo$z), 1, 0)
 
  On 5/10/07, Greg Tarpinian [EMAIL PROTECTED] wrote:
   I have a question that must have a simple answer (but eludes me).
   I need a row-by-row logical comparison across three numeric variables
   in
   a data frame: foo$x, foo$y, foo$z.  The logic is
  
 if( x  y || x  z ) 1 else 0
  
   for a particular row.
  
   It is simple and very inefficient to use for(i in 1:length(foo$x)){ }
   loops.  How can I accomplish this using sappy( ) / lapply( ) / apply( )
   or some other more efficient method?
  
   Thank you in advance,
  
  Greg
  
   __
   R-help@stat.math.ethz.ch 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.
  
 
 
  --
  Jim Holtman
  Cincinnati, OH
  +1 513 646 9390
 
  What is the problem you are trying to solve?
 
  __
  R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] apply problem

2007-04-13 Thread aedin culhane
Dear R-Help
I am running apply on a data.frame containing factors and numeric 
columns.  It appears to convert are columns into as.character? Does it 
convert data.frame into matrix? Is this expected? I wish it to recognise 
numerical columns and round numbers.  Can I use another function instead 
of apply, or should I use a for loop in the case?

  summary(xmat)
A   B C D
  Min.   :  1.0   414:  1   Stage 2:  5   Min.   :-0.075369
  1st Qu.:113.8   422:  1   Stage 3:  6   1st Qu.:-0.018102
  Median :226.5   426:  1   Stage 4:441   Median :-0.003033
  Mean   :226.5   436:  1 Mean   : 0.008007
  3rd Qu.:339.2   460:  1 3rd Qu.: 0.015499
  Max.   :452.0   462:  1 Max.   : 0.400578
  (Other):446
EFG
  Min.   :0.2345   Min.   :0.9808   Min.   :0.01558
  1st Qu.:0.2840   1st Qu.:0.9899   1st Qu.:0.02352
  Median :0.3265   Median :0.9965   Median :0.02966
  Mean   :0.3690   Mean   :1.0079   Mean   :0.03580
  3rd Qu.:0.3859   3rd Qu.:1.0129   3rd Qu.:0.03980
  Max.   :2.0422   Max.   :1.3742   Max.   :0.20062

  for(i in 1:7) print(class(xmat[,i]))
[1] integer
[1] factor
[1] factor
[1] numeric
[1] numeric
[1] numeric
[1] numeric

  apply(xmat, 2, class)
   A   B   C   D   E   F
character character character character character character
   G
character



Thanks for your help
Aedin

__
[EMAIL PROTECTED] 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] apply problem

2007-04-13 Thread Prof Brian Ripley
On Fri, 13 Apr 2007, aedin culhane wrote:

 Dear R-Help
 I am running apply on a data.frame containing factors and numeric
 columns.  It appears to convert are columns into as.character? Does it
 convert data.frame into matrix? Is this expected? I wish it to recognise

Yes, and quite explicit on the help page

Arguments:

X: the array to be used.
   ^
  If 'X' is not an array but has a dimension attribute, 'apply'
  attempts to coerce it to an array via 'as.matrix' if it is
  two-dimensional (e.g., data frames) or via 'as.array'.

I am baffled as to how you managed to miss this, as it is part of the 
homework the posting guide asked you to do *before* posting.



 numerical columns and round numbers.  Can I use another function instead
 of apply, or should I use a for loop in the case?

You haven;t told us _what_ you want to do, but lapply works by column.

  summary(xmat)
A   B C D
  Min.   :  1.0   414:  1   Stage 2:  5   Min.   :-0.075369
  1st Qu.:113.8   422:  1   Stage 3:  6   1st Qu.:-0.018102
  Median :226.5   426:  1   Stage 4:441   Median :-0.003033
  Mean   :226.5   436:  1 Mean   : 0.008007
  3rd Qu.:339.2   460:  1 3rd Qu.: 0.015499
  Max.   :452.0   462:  1 Max.   : 0.400578
  (Other):446
EFG
  Min.   :0.2345   Min.   :0.9808   Min.   :0.01558
  1st Qu.:0.2840   1st Qu.:0.9899   1st Qu.:0.02352
  Median :0.3265   Median :0.9965   Median :0.02966
  Mean   :0.3690   Mean   :1.0079   Mean   :0.03580
  3rd Qu.:0.3859   3rd Qu.:1.0129   3rd Qu.:0.03980
  Max.   :2.0422   Max.   :1.3742   Max.   :0.20062

  for(i in 1:7) print(class(xmat[,i]))
 [1] integer
 [1] factor
 [1] factor
 [1] numeric
 [1] numeric
 [1] numeric
 [1] numeric

Better, sapply(xmat, class).

  apply(xmat, 2, class)
   A   B   C   D   E   F
 character character character character character character
   G
 character

Well, all columns of a matrix are of the same class.


-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
[EMAIL PROTECTED] 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] apply problem

2007-04-13 Thread Stephen Tucker
?apply says

If X is not an array but has a dimension attribute, apply attempts to coerce
it to an array via as.matrix if it is two-dimensional (e.g., data frames). .
.

It would probably be easiest with a FOR-LOOP, but you could also try
something like the code below (and insert your operations in #...).

myfunc - function(x,classOfX) {
  x - as.data.frame(t(x))
  factvars - which(classOfX==factor)
  x[,factvars] - lapply(x[,factvars],factor)
  for( i in seq(along=x) ) x[,i] - as(x[,i],Class=classOfX[i])
  # ...
  return(x)
}
x - data.frame(a=as.integer(1:10),b=factor(letters[1:10]),c=runif(10))
Fold - function(f,x,L) for(e in L) x - f(x,e)
y - Fold(rbind,vector(),apply(x,1,myfunc,rapply(x,class)))

 rapply(x,class)
a b c 
integer  factor numeric 
 rapply(y,class)
a b c 
integer  factor numeric 



--- aedin culhane [EMAIL PROTECTED] wrote:

 Dear R-Help
 I am running apply on a data.frame containing factors and numeric 
 columns.  It appears to convert are columns into as.character? Does it 
 convert data.frame into matrix? Is this expected? I wish it to recognise 
 numerical columns and round numbers.  Can I use another function instead 
 of apply, or should I use a for loop in the case?
 
   summary(xmat)
 A   B C D
   Min.   :  1.0   414:  1   Stage 2:  5   Min.   :-0.075369
   1st Qu.:113.8   422:  1   Stage 3:  6   1st Qu.:-0.018102
   Median :226.5   426:  1   Stage 4:441   Median :-0.003033
   Mean   :226.5   436:  1 Mean   : 0.008007
   3rd Qu.:339.2   460:  1 3rd Qu.: 0.015499
   Max.   :452.0   462:  1 Max.   : 0.400578
   (Other):446
 EFG
   Min.   :0.2345   Min.   :0.9808   Min.   :0.01558
   1st Qu.:0.2840   1st Qu.:0.9899   1st Qu.:0.02352
   Median :0.3265   Median :0.9965   Median :0.02966
   Mean   :0.3690   Mean   :1.0079   Mean   :0.03580
   3rd Qu.:0.3859   3rd Qu.:1.0129   3rd Qu.:0.03980
   Max.   :2.0422   Max.   :1.3742   Max.   :0.20062
 
   for(i in 1:7) print(class(xmat[,i]))
 [1] integer
 [1] factor
 [1] factor
 [1] numeric
 [1] numeric
 [1] numeric
 [1] numeric
 
   apply(xmat, 2, class)
A   B   C   D   E   F
 character character character character character character
G
 character
 
 
 
 Thanks for your help
 Aedin
 
 __
 [EMAIL PROTECTED] 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.


__
[EMAIL PROTECTED] 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] apply problem

2007-04-13 Thread Seth Falcon
aedin culhane [EMAIL PROTECTED] writes:

 Dear R-Help
 I am running apply on a data.frame containing factors and numeric 
 columns.  It appears to convert are columns into as.character? Does it 
 convert data.frame into matrix? Is this expected? I wish it to recognise 
 numerical columns and round numbers.  Can I use another function instead 
 of apply, or should I use a for loop in the case?

If you want to modify the data.frame object, a for loop will likely be
the best bet.  As noted in other replies, lapply will operate on the
columns of a data.frame since a data.frame is a list.  But the return
value will be a list, not a data.frame.

I think for loops get a bad wrap.  There are times when they are
appropriate and even optimal in R programming.

+ seth

-- 
Seth Falcon | Computational Biology | Fred Hutchinson Cancer Research Center
http://bioconductor.org

__
[EMAIL PROTECTED] 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] apply ? function doesnt create object

2007-03-03 Thread bunny , lautloscrew.com
hello,
i have written a function to extract certain lines from a matrix. the  
result is a matrix with 6 cols, named dynamically according to the  
functions arguments.

the problem is now, that i'm not able to return the resultmatrix for  
further use. the object is not being created.

example from my console:

getans(27,27)
[...]
[189,] 3969  161   27101
[190,] 2142   87   27101
[191,] 1318   52   27101
[192,] 2881  120   27101
[193,] 3669  152   27101
[194,] 5653  229   27101
[195,] 2308   95   27102
[196,] 4942  200   27101
the matrix ansblock27 contains answers to block 27
  ansblock27
error: object ansblock27 not found
 



the code of the function:

getans = function(x=qids,bnr=1,type=block)
{
#generate name of matrix
matnam=paste(ans,type,as.character(bnr),sep=)

#display result matrix
show(assign(matnam,matrix(as.numeric(as.matrix(allans[(allans[, 3] %in 
% x), , drop = FALSE])),ncol=dim(allans)[2])))

#create result matrix
assign(matnam,matrix(as.numeric(as.matrix(allans[(allans[, 3] %in%  
x), , drop = FALSE])),ncol=dim(allans)[2]))

#print info
cat(the matrix,matnam,contains answers to,type,as.character(bnr))   

}

i have tried to use return, but had no success.

what else can i do?

thx in advance, and have a nice weekend
regards,


[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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] apply ? function doesnt create object

2007-03-03 Thread Charilaos Skiadas
On Mar 3, 2007, at 4:28 PM, bunny , lautloscrew.com wrote:

Please use - for assignments instead of = :

 getans = function(x=qids,bnr=1,type=block)
   {
 #generate name of matrix
 matnam=paste(ans,type,as.character(bnr),sep=)

 #display result matrix
 show(assign(matnam,matrix(as.numeric(as.matrix(allans[(allans[, 3] %in
 % x), , drop = FALSE])),ncol=dim(allans)[2])))

You are assigning things twice here.

 #create result matrix
 assign(matnam,matrix(as.numeric(as.matrix(allans[(allans[, 3] %in%
 x), , drop = FALSE])),ncol=dim(allans)[2]))

The documentation for assign makes it pretty clear that the  
assignment happens by default in the current environment, so it will  
be local to the function unless you alter the call. The description  
there, along with the examples, and a study of environments, should  
provide you with the answer.

 #print info
 cat(the matrix,matnam,contains answers to,type,as.character 
 (bnr))
   
 }

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College

__
R-help@stat.math.ethz.ch 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] apply for list or list - array

2006-09-19 Thread Ahn ChaeHyung
Dear all,

I have the following list, aa, composed of two 3*3 tables.  I would like 
to use apply function to summarize it, but apply cannot handle list.  
I want to do it without using any interation.

1. Is there any function like apply for list?
2. Is there any way to transform that list to a 2*3*3 array?

thanks

cahn

-
aa
[[1]]
 [,1] [,2] [,3]
[1,]131
[2,]201
[3,]110

[[2]]
 [,1] [,2] [,3]
[1,]131
[2,]201
[3,]110


I would like to avoid any iteration



-
Ahn, Chaehyung (cahn) Ph.D.
Alpha-Gamma Technologies Inc.
My Home: http://blog.naver.com/cahn88

__
R-help@stat.math.ethz.ch 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] apply for list or list - array

2006-09-19 Thread Uwe Ligges
Ahn ChaeHyung wrote:

 Dear all,
 
 I have the following list, aa, composed of two 3*3 tables.  I would like 
 to use apply function to summarize it, but apply cannot handle list.  
 I want to do it without using any interation.
 
 1. Is there any function like apply for list?
 2. Is there any way to transform that list to a 2*3*3 array?


Use lapply() or sapply().
Example to transpose both matrices:
lapply(a, t)


Uwe Ligges



 thanks
 
 cahn
 
 -
 
aa
 
 [[1]]
  [,1] [,2] [,3]
 [1,]131
 [2,]201
 [3,]110
 
 [[2]]
  [,1] [,2] [,3]
 [1,]131
 [2,]201
 [3,]110
 
 
 I would like to avoid any iteration
 
 
 
 -
 Ahn, Chaehyung (cahn) Ph.D.
 Alpha-Gamma Technologies Inc.
 My Home: http://blog.naver.com/cahn88
 
 __
 R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] apply for list or list - array

2006-09-19 Thread Roger Bivand
On Mon, 18 Sep 2006, Ahn ChaeHyung wrote:

 Dear all,
 
 I have the following list, aa, composed of two 3*3 tables.  I would like 
 to use apply function to summarize it, but apply cannot handle list.  
 I want to do it without using any interation.
 
 1. Is there any function like apply for list?

?lapply, ?sapply

 2. Is there any way to transform that list to a 2*3*3 array?
 

Among other possibilities:

library(abind)
aa - list(m1=matrix(rnorm(9), 3, 3), m2=matrix(runif(9), 3, 3))
a1 - abind(aa, along=3)
str(a1)
dim(a1)


 thanks
 
 cahn
 
 -
 aa
 [[1]]
  [,1] [,2] [,3]
 [1,]131
 [2,]201
 [3,]110
 
 [[2]]
  [,1] [,2] [,3]
 [1,]131
 [2,]201
 [3,]110
 
 
 I would like to avoid any iteration
 
 
 
 -
 Ahn, Chaehyung (cahn) Ph.D.
 Alpha-Gamma Technologies Inc.
 My Home: http://blog.naver.com/cahn88
 
 __
 R-help@stat.math.ethz.ch 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.
 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: [EMAIL PROTECTED]

__
R-help@stat.math.ethz.ch 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] apply for list or list - array

2006-09-19 Thread Dimitris Rizopoulos
for lists look at ?lapply() and ?sapply(); for your 2nd question try 
something like:

array(unlist(aa), dim = c(3, 3, 2))


I hope it helps.

Best,
Dimitris


Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
 http://www.student.kuleuven.be/~m0390867/dimitris.htm


- Original Message - 
From: Ahn ChaeHyung [EMAIL PROTECTED]
To: r-help@stat.math.ethz.ch
Sent: Tuesday, September 19, 2006 3:32 AM
Subject: [R] apply for list or list - array


 Dear all,

 I have the following list, aa, composed of two 3*3 tables.  I 
 would like
 to use apply function to summarize it, but apply cannot handle 
 list.
 I want to do it without using any interation.

 1. Is there any function like apply for list?
 2. Is there any way to transform that list to a 2*3*3 array?

 thanks

 cahn

 -
aa
 [[1]]
 [,1] [,2] [,3]
 [1,]131
 [2,]201
 [3,]110

 [[2]]
 [,1] [,2] [,3]
 [1,]131
 [2,]201
 [3,]110


 I would like to avoid any iteration



 -
 Ahn, Chaehyung (cahn) Ph.D.
 Alpha-Gamma Technologies Inc.
 My Home: http://blog.naver.com/cahn88

 __
 R-help@stat.math.ethz.ch 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.
 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

__
R-help@stat.math.ethz.ch 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] apply least angle regression to generalized linear models

2006-08-18 Thread Mike Wolfgang
Hello list,

I've been searching around trying to find whether somebody has written such
a package of least angle regression on generalized linear models, like what
Lasso2 package does. The extension to generalized linear models is briefly
discussed in the comment by D. Madigan and G. Ridgeway. Is such a package
available? Thanks,

Mike

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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] apply least angle regression to generalized linear models

2006-08-18 Thread Marc Schwartz (via MN)
On Fri, 2006-08-18 at 11:17 -0400, Mike Wolfgang wrote:
 Hello list,
 
 I've been searching around trying to find whether somebody has written such
 a package of least angle regression on generalized linear models, like what
 Lasso2 package does. The extension to generalized linear models is briefly
 discussed in the comment by D. Madigan and G. Ridgeway. Is such a package
 available? Thanks,
 
 Mike

See the aptly named 'lars' package on CRAN and the attendant paper here:

  http://www-stat.stanford.edu/~hastie/Papers/LARS/LeastAngle_2002.pdf

You might also want to review Professor Hastie's presentation at useR!
2006 this past spring:

  http://www.r-project.org/useR-2006/Slides/Hastie.pdf

HTH,

Marc Schwartz

__
R-help@stat.math.ethz.ch 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] apply least angle regression to generalized linear models

2006-08-18 Thread Liaw, Andy
I believe `lars' does not currently fit glms.  For that you'll probably need
to look at `glar', at:

http://www.insightful.com/Hesterberg/glars/default.asp

HTH,
Andy 

From: Marc Schwartz
 
 On Fri, 2006-08-18 at 11:17 -0400, Mike Wolfgang wrote:
  Hello list,
  
  I've been searching around trying to find whether somebody 
 has written 
  such a package of least angle regression on generalized 
 linear models, 
  like what
  Lasso2 package does. The extension to generalized linear models is 
  briefly discussed in the comment by D. Madigan and G. Ridgeway. Is 
  such a package available? Thanks,
  
  Mike
 
 See the aptly named 'lars' package on CRAN and the attendant 
 paper here:
 
   http://www-stat.stanford.edu/~hastie/Papers/LARS/LeastAngle_2002.pdf
 
 You might also want to review Professor Hastie's presentation at useR!
 2006 this past spring:
 
   http://www.r-project.org/useR-2006/Slides/Hastie.pdf
 
 HTH,
 
 Marc Schwartz
 
 __
 R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] apply least angle regression to generalized linear models

2006-08-18 Thread Marc Schwartz (via MN)
Andy,

Upon further review of the documentation for lars, you are correct.

Thanks for the pointer to the work by Tim et al.

Regards,

Marc

On Fri, 2006-08-18 at 12:48 -0400, Liaw, Andy wrote:
 I believe `lars' does not currently fit glms.  For that you'll probably need
 to look at `glar', at:
 
 http://www.insightful.com/Hesterberg/glars/default.asp
 
 HTH,
 Andy 
 
 From: Marc Schwartz
  
  On Fri, 2006-08-18 at 11:17 -0400, Mike Wolfgang wrote:
   Hello list,
   
   I've been searching around trying to find whether somebody 
  has written 
   such a package of least angle regression on generalized 
  linear models, 
   like what
   Lasso2 package does. The extension to generalized linear models is 
   briefly discussed in the comment by D. Madigan and G. Ridgeway. Is 
   such a package available? Thanks,
   
   Mike
  
  See the aptly named 'lars' package on CRAN and the attendant 
  paper here:
  
http://www-stat.stanford.edu/~hastie/Papers/LARS/LeastAngle_2002.pdf
  
  You might also want to review Professor Hastie's presentation at useR!
  2006 this past spring:
  
http://www.r-project.org/useR-2006/Slides/Hastie.pdf
  
  HTH,
  
  Marc Schwartz
  
  __
  R-help@stat.math.ethz.ch 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.
  
  
 
 
 --
 Notice:  This e-mail message, together with any attachment...{{dropped}}

__
R-help@stat.math.ethz.ch 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] apply a function to several lists' components

2006-06-30 Thread Taka Matzmoto
Dear R-user
I have 100 lists.
Each list has several components.
For example,

data1
$a
[1] 1 2

$b
[1] 3 4

$c
[1] 5

There are data1, data2,, data100. All lists have the same number and the 
same name of components.


Is there any function I can use for applying to only a specific component 
across 100 lists?
(e.g.,  taking mean of $c acorss 100 lists) or do I need to write my own 
function for that?

Thank you.

Taka,

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply a function to several lists' components

2006-06-30 Thread JeeBee

Maybe this helps

( data1 = list(a=c(1,2), b=c(3,4), c=c(5,6,7)) )
( data2 = list(a=c(10,11), b=c(30,40), c=c(70,80)) )

cc - NULL
for(data in ls(pattern=^data[0-9]+$)) {
  cc - c(cc, with(get(data), c))
}

mean(cc)

JeeBee.


On Fri, 30 Jun 2006 09:50:51 -0500, Taka Matzmoto wrote:

 Dear R-user
 I have 100 lists.
 Each list has several components.
 For example,
 
data1
 $a
 [1] 1 2
 
 $b
 [1] 3 4
 
 $c
 [1] 5
 
 There are data1, data2,, data100. All lists have the same number and the 
 same name of components.
 
 
 Is there any function I can use for applying to only a specific component 
 across 100 lists?
 (e.g.,  taking mean of $c acorss 100 lists) or do I need to write my own 
 function for that?
 
 Thank you.
 
 Taka,
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply, apply speed vs. traditional looping mechanisms

2006-05-14 Thread John Sorkin
Can someone tell me why apply (and apply) are faster in performing
repeated operations than a for (or do) loop? I am looking for a
technical explanation.
Thanks,
John

John Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
Baltimore VA Medical Center GRECC and
University of Maryland School of Medicine Claude Pepper OAIC

University of Maryland School of Medicine
Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524

410-605-7119
[EMAIL PROTECTED]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply, apply speed vs. traditional looping mechanisms

2006-05-14 Thread Prof Brian Ripley
On Sun, 14 May 2006, John Sorkin wrote:

 Can someone tell me why apply (and apply) are faster in performing
 repeated operations than a for (or do) loop? I am looking for a
 technical explanation.

apply() is just a wrapper for a for loop.  So it is not faster that at 
least one implementation using a for loop: it may be neater and easier to 
understand than an explicit for loop.

I don't understand why you used 'apply' twice here.

lapply() can be faster than a carefully crafted for() loop (since C-level 
code is more efficient in memory allocation), but its main rationale is 
clarity (and especially to avoid traps like

ans - vector(list, n)
for(i in 1:n) ans[[i]] - fun(i)

if n turns out to be zero or fun(i) to be NULL).

-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply, apply speed vs. traditional looping mechanisms

2006-05-14 Thread John Sorkin
Prof Ripley,
Many thanks for your reply. I did not mean to use apply twice, I meant
to ask about apply and lapply. I understand your comment re: apply. I
assume your comment about lapply is meant to mean that lapply is
implemented in C code and therefore should be faster than a loop written
in R.

I would also like to take this opportunity to thank you for your may
contributions to R and this list.

Thank you,
John Sorkin 


John Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
Baltimore VA Medical Center GRECC and
University of Maryland School of Medicine Claude Pepper OAIC

University of Maryland School of Medicine
Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524

410-605-7119
[EMAIL PROTECTED]

 Prof Brian Ripley [EMAIL PROTECTED] 05/14/06 4:26 PM 
On Sun, 14 May 2006, John Sorkin wrote:

 Can someone tell me why apply (and apply) are faster in performing
 repeated operations than a for (or do) loop? I am looking for a
 technical explanation.

apply() is just a wrapper for a for loop.  So it is not faster that at

least one implementation using a for loop: it may be neater and easier
to 
understand than an explicit for loop.

I don't understand why you used 'apply' twice here.

lapply() can be faster than a carefully crafted for() loop (since
C-level 
code is more efficient in memory allocation), but its main rationale is

clarity (and especially to avoid traps like

ans - vector(list, n)
for(i in 1:n) ans[[i]] - fun(i)

if n turns out to be zero or fun(i) to be NULL).

-- 
Brian D. Ripley,  [EMAIL PROTECTED] 
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/ 
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply

2006-05-13 Thread karim\.regh
Dear Sir,
I’am a new user of “R” Software. I thank you very much for this Software.
By running the “R” 2.3.0 software I have encountred aproblem:
I have downloaded the tseriesChaos package from CRAN. When I try to run any 
function of the the tseriesChaos package for example, the c2 function the “R” 
software diplays on the screen ‘error, impossible to find the c2 function).
Please help me! What shall I do to add on this package and run it correctly?
Why the Software displays ‘error, impossible to find the c2 function’? Is it 
caused by a damaged version of the “R” 2.3.0?
I thank you in advance and I am waiting forward your e-mail

This is my e-mail: [EMAIL PROTECTED]

Accédez au courrier électronique de La Poste : www.laposte.net ; 
3615 LAPOSTENET (0,34 €/mn) ; tél : 08 92 68 13 50 (0,34€/mn)



[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Re: [R] apply

2006-05-13 Thread Marc Schwartz
On Sat, 2006-05-13 at 23:02 +0200, karim.regh wrote:
 Dear Sir,
 Iam a new user of R Software. I thank you very much for this Software.
 By running the R 2.3.0 software I have encountred aproblem:
 I have downloaded the tseriesChaos package from CRAN. When I try to
 run any function of the the tseriesChaos package for example, the c2
 function the R software diplays on the screen error, impossible to
 find the c2 function).
 Please help me! What shall I do to add on this package and run it
 correctly?
 Why the Software displays error, impossible to find the c2 function?
 Is it caused by a damaged version of the R 2.3.0?
 I thank you in advance and I am waiting forward your e-mail

Did you use:

  library(tseriesChaos)

before trying to use the functions that are contained in it?

See R FAQ:

  7.30 I installed a package but the functions are not there

Reading the Posting Guide as noted at the bottom of each e-mail coming
through the list would enable you to solve many basic problems such as
this.

HTH,

Marc Schwartz

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply(table) miss factor structure

2006-04-19 Thread Cézar Freitas
Hi, all.
I didn't find something similar to this problem in
past list.

I have a data frame (named restr) where some columns
are factors, like you can see:

 table(restr[,p1])
 0  1  2  3  4  5 
 0 26  1  0  1  0 
 table(restr[,p2])
 0  1  2  3  4  5  6 
 0 13 11  1  2  1  0 

When I use apply, the factor structure is missed:

 apply(restr[,c(p1,p2)], 2, table)
$p1

 1  2  4 
26  1  1 

$p2

 1  2  3  4  5 
13 11  1  2  1 

Can I use a matricial (like apply) manner to do this
holding the factor structure (the zero-counts must be
displayed)?

Thanks,
Cezar Freitas
Unicamp - Brasil

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply(table) miss factor structure

2006-04-19 Thread Dimitris Rizopoulos
you should use:

lapply(restr[c(p1,p2)], table)


I hope it helps.

Best,
Dimitris


Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
 http://www.student.kuleuven.be/~m0390867/dimitris.htm


- Original Message - 
From: Cézar Freitas [EMAIL PROTECTED]
To: r-help@stat.math.ethz.ch
Sent: Wednesday, April 19, 2006 3:50 PM
Subject: [R] apply(table) miss factor structure


 Hi, all.
 I didn't find something similar to this problem in
 past list.

 I have a data frame (named restr) where some columns
 are factors, like you can see:

 table(restr[,p1])
 0  1  2  3  4  5
 0 26  1  0  1  0
 table(restr[,p2])
 0  1  2  3  4  5  6
 0 13 11  1  2  1  0

 When I use apply, the factor structure is missed:

 apply(restr[,c(p1,p2)], 2, table)
 $p1

 1  2  4
 26  1  1

 $p2

 1  2  3  4  5
 13 11  1  2  1

 Can I use a matricial (like apply) manner to do this
 holding the factor structure (the zero-counts must be
 displayed)?

 Thanks,
 Cezar Freitas
 Unicamp - Brasil

 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! 
 http://www.R-project.org/posting-guide.html
 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply(ing) to sum subset of a vector

2006-03-27 Thread Fred J.
Dear R users
 
 I am trying to sum selective elements of a vector but my solution
 is not cutting it.
 
 Example:
  g - 1:5;
 
  from - 1:3;
  to - 3:5;
 from to
 1   3
 2   4
 3   5
 
 so I expect 3 sums from g
 1+2+3  that is 1 to 3 of g
 2+3+4  that is 2 to 4 of g
 3+4+5  that is 3 to 5 of g
 
 my solution will not work.
 sum.em - function(g, c1, c2) sum(g[c1:c2])
 apply(g, 1, sum.em, ...) I don't think so because apply is not
 aware of the from and to. and if I f - list(g, from, to) that will not fit 
with the second arg of apply.
 
 thank you
 

-

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply(ing) to sum subset of a vector

2006-03-27 Thread jim holtman
create a matrix and then use apply:

 g - 1:5;

  from - 1:3;
  to - 3:5;

 index - cbind(from,to)
 apply(index, 1, function(x) sum(g[x[1]:x[2]]))
[1]  6  9 12




On 3/27/06, Fred J. [EMAIL PROTECTED] wrote:

 Dear R users

 I am trying to sum selective elements of a vector but my solution
 is not cutting it.

 Example:
  g - 1:5;

  from - 1:3;
  to - 3:5;
 from to
 1   3
 2   4
 3   5

 so I expect 3 sums from g
 1+2+3  that is 1 to 3 of g
 2+3+4  that is 2 to 4 of g
 3+4+5  that is 3 to 5 of g

 my solution will not work.
 sum.em - function(g, c1, c2) sum(g[c1:c2])
 apply(g, 1, sum.em, ...) I don't think so because apply is not
 aware of the from and to. and if I f - list(g, from, to) that will not
 fit with the second arg of apply.

 thank you


 -

[[alternative HTML version deleted]]

 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide!
 http://www.R-project.org/posting-guide.html




--
Jim Holtman
Cincinnati, OH
+1 513 646 9390 (Cell)
+1 513 247 0281 (Home)

What the problem you are trying to solve?

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply(ing) to sum subset of a vector

2006-03-27 Thread Jacques VESLOT
apply(cbind(from,to), 1, function(x) sum(g[x[1]:x[2]]))

Fred J. a écrit :

Dear R users
 
 I am trying to sum selective elements of a vector but my solution
 is not cutting it.
 
 Example:
  g - 1:5;
 
  from - 1:3;
  to - 3:5;
 from to
 1   3
 2   4
 3   5
 
 so I expect 3 sums from g
 1+2+3  that is 1 to 3 of g
 2+3+4  that is 2 to 4 of g
 3+4+5  that is 3 to 5 of g
 
 my solution will not work.
 sum.em - function(g, c1, c2) sum(g[c1:c2])
 apply(g, 1, sum.em, ...) I don't think so because apply is not
 aware of the from and to. and if I f - list(g, from, to) that will not fit 
 with the second arg of apply.
 
 thank you
 
   
-

   [[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

  


__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] Apply and more argumnts function

2006-03-04 Thread Andrej Kastrin
Dear useRs,

shame on me, but I have no idea how to apply two arguments function on 
my data. I have 2 vectors, 'n' and 'm' and  the function below:

n - c(10,30,50,1000)
m - c(10,50,100,200)

MonteCarlo - function(n,m){
  temp - NULL
  for(i in 1:m){
temp - c(temp,walk(n)) # walk is external function
  }
  return(c(n,m,mean(temp),sd(temp)))
}

Now I have to calculate mean(temp) and sd(temp) for each pair of 
corresponding elements in vectors.

Thanks in advance, Andrej

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply

2006-01-13 Thread Guenther, Cameron
Hello,
I have a dataset d which is
 d
 pop catch 
1   66462.01 10807.757 
2   87486.73 46257.885 
3   57211.64  9345.058 
4   71321.62  4892.868 
5  100024.89 27334.248 
6  104504.91 48535.092 
7   95295.51 39348.195 
8   93737.35 34343.489 
9   89375.05 28750.743 
10  95312.65 30755.064 
11 100888.17 55404.370 
12  84790.23 37751.074 
13  81129.82 29277.443 
14  69797.09 21500.398 
15  61690.34 18199.664 
16  60671.08 19349.051 
17  62852.57 16300.982 
18  90646.32 34793.148 

And a function opt1:

opt1 - function (x) {
  Z - x + M
  c.hat - (x/Z)*pop*(1-exp(-Z))
  y - (catch - c.hat)^2
  return(y)
  }

And define M = 0.25 
For each row I want to return a value F that is a minimization of the
opt1 function

I have tried many variations of:
d$F-apply(d,2,optim(0.3,opt1,method=BFGS)
and even:
For (I in 1:length(pop))apply(d,2,optim(0.3,opt1,method=BFGS))

Every time I get the same error message

Error in optim(0.3, opt1, method = BFGS) : 
objective function in optim evaluates to length 18 not 1

So the apply function is returning 18 values of F which I want but the
function only wants to return 1 value.

Any Suggestions.
Thanks,
Cam

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply

2006-01-13 Thread Berton Gunter
You are not calling apply() properly. Please read relevant reference
material carefully. You might also wish to pick up one of the several good
books on R (check CRAN website) -- I like VR's S PROGRAMMING.

I did not go through your example in detail, but your apply() call should be
of the form

apply(d,2,function(x)optim(x,...))

You may or may not get into scoping problems with the opt1 argument and have
to pass it in explicitly -- I can't remember how things work with optim. 

-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
 
The business of the statistician is to catalyze the scientific learning
process.  - George E. P. Box
 
 

 -Original Message-
 From: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] On Behalf Of 
 Guenther, Cameron
 Sent: Friday, January 13, 2006 11:03 AM
 To: [EMAIL PROTECTED]
 Subject: [R] apply
 
 Hello,
 I have a dataset d which is
  d
  pop catch 
 1   66462.01 10807.757 
 2   87486.73 46257.885 
 3   57211.64  9345.058 
 4   71321.62  4892.868 
 5  100024.89 27334.248 
 6  104504.91 48535.092 
 7   95295.51 39348.195 
 8   93737.35 34343.489 
 9   89375.05 28750.743 
 10  95312.65 30755.064 
 11 100888.17 55404.370 
 12  84790.23 37751.074 
 13  81129.82 29277.443 
 14  69797.09 21500.398 
 15  61690.34 18199.664 
 16  60671.08 19349.051 
 17  62852.57 16300.982 
 18  90646.32 34793.148 
 
 And a function opt1:
 
 opt1 - function (x) {
   Z - x + M
   c.hat - (x/Z)*pop*(1-exp(-Z))
   y - (catch - c.hat)^2
   return(y)
   }
 
 And define M = 0.25 
 For each row I want to return a value F that is a minimization of the
 opt1 function
 
 I have tried many variations of:
 d$F-apply(d,2,optim(0.3,opt1,method=BFGS)
 and even:
 For (I in 1:length(pop))apply(d,2,optim(0.3,opt1,method=BFGS))
 
 Every time I get the same error message
 
 Error in optim(0.3, opt1, method = BFGS) : 
 objective function in optim evaluates to length 18 not 1
 
 So the apply function is returning 18 values of F which I want but the
 function only wants to return 1 value.
 
 Any Suggestions.
 Thanks,
 Cam
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! 
 http://www.R-project.org/posting-guide.html


__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply

2006-01-13 Thread Sundar Dorai-Raj
In addition the error message explicitly says:

Error in optim(0.3, opt1, method = BFGS) :
  objective function in optim evaluates to length 18 not 1

And from the objective function we see

opt1 - function (x) {
   Z - x + M
   c.hat - (x/Z)*pop*(1-exp(-Z))
   y - (catch - c.hat)^2
   return(y)
}

The last line should be replaced by:

sum(y)

rather than return(y). See ?optim, ?apply, and the reference Bert 
gives below.

HTH,

--sundar


Berton Gunter wrote:
 You are not calling apply() properly. Please read relevant reference
 material carefully. You might also wish to pick up one of the several good
 books on R (check CRAN website) -- I like VR's S PROGRAMMING.
 
 I did not go through your example in detail, but your apply() call should be
 of the form
 
 apply(d,2,function(x)optim(x,...))
 
 You may or may not get into scoping problems with the opt1 argument and have
 to pass it in explicitly -- I can't remember how things work with optim. 
 
 -- Bert Gunter
 Genentech Non-Clinical Statistics
 South San Francisco, CA
  
 The business of the statistician is to catalyze the scientific learning
 process.  - George E. P. Box
  
  
 
 
-Original Message-
From: [EMAIL PROTECTED] 
[mailto:[EMAIL PROTECTED] On Behalf Of 
Guenther, Cameron
Sent: Friday, January 13, 2006 11:03 AM
To: [EMAIL PROTECTED]
Subject: [R] apply

Hello,
I have a dataset d which is
 d
 pop catch 
1   66462.01 10807.757 
2   87486.73 46257.885 
3   57211.64  9345.058 
4   71321.62  4892.868 
5  100024.89 27334.248 
6  104504.91 48535.092 
7   95295.51 39348.195 
8   93737.35 34343.489 
9   89375.05 28750.743 
10  95312.65 30755.064 
11 100888.17 55404.370 
12  84790.23 37751.074 
13  81129.82 29277.443 
14  69797.09 21500.398 
15  61690.34 18199.664 
16  60671.08 19349.051 
17  62852.57 16300.982 
18  90646.32 34793.148 

And a function opt1:

opt1 - function (x) {
  Z - x + M
  c.hat - (x/Z)*pop*(1-exp(-Z))
  y - (catch - c.hat)^2
  return(y)
  }

And define M = 0.25 
For each row I want to return a value F that is a minimization of the
opt1 function

I have tried many variations of:
d$F-apply(d,2,optim(0.3,opt1,method=BFGS)
and even:
For (I in 1:length(pop))apply(d,2,optim(0.3,opt1,method=BFGS))

Every time I get the same error message

Error in optim(0.3, opt1, method = BFGS) : 
objective function in optim evaluates to length 18 not 1

So the apply function is returning 18 values of F which I want but the
function only wants to return 1 value.

Any Suggestions.
Thanks,
Cam

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! 
http://www.R-project.org/posting-guide.html

 
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply() and dropped dimensions

2005-12-05 Thread Robin Hankin
Hi

I am having difficulty with apply().  I want apply() to return a
matrix, but sometimes a vector is returned.

   Toy example follows.

Function jj()  takes a couple of matrices m1 and m2 as arguments
and returns a matrix with r rows and c columns where r=nrow(m2)
and c=nrow(m1).


jj - function(m1,m2,f,...){
   apply(m1, 1, function(y) {
   apply(m2, 1, function(x) {
  f(x, y, ...)
})
  })
}

R jj(matrix(1:20,4,5),matrix(1:10,2,5),f=sum)
  [,1] [,2] [,3] [,4]
[1,]   70   75   80   85
[2,]   75   80   85   90

intended behaviour:  matrix returned.  [eg, 70 = sum(m1[1,],m2[1,]) ]

R jj(matrix(1:20,4,5),matrix(1:5,1,5),f=sum)
[1] 60 65 70 75
R

not intended behaviour: vector returned.


How do I rewrite jj() so that it consistently returns a matrix?


--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply() and dropped dimensions

2005-12-05 Thread Berwin A Turlach
G'day Robin,

 RH == Robin Hankin [EMAIL PROTECTED] writes:

RH How do I rewrite jj() so that it consistently returns a
RH matrix?
How about explicitly returning a matrix with the desired dimensions?

 jj
function(m1,m2,f,...)
matrix(apply(m1, 1, function(y) 
 apply(m2, 1, function(x)
   f(x, y, ...)
   )),
   nrow=nrow(m2), ncol=nrow(m1))

 jj(matrix(1:20,4,5),matrix(1:10,2,5),f=sum)
 [,1] [,2] [,3] [,4]
[1,]   70   75   80   85
[2,]   75   80   85   90

 jj(matrix(1:20,4,5),matrix(1:5,1,5),f=sum)
 [,1] [,2] [,3] [,4]
[1,]   60   65   70   75


Or, using the outer command:

 jj
function(m1,m2,f,...)
outer(1:nrow(m2), 1:nrow(m1), function(i,j) apply(cbind(i,j), 1, 
function(ii)  f(m2[ii[1],], m1[ii[2],], ...)))

 jj(matrix(1:20,4,5),matrix(1:10,2,5),f=sum)
 [,1] [,2] [,3] [,4]
[1,]   70   75   80   85
[2,]   75   80   85   90

 jj(matrix(1:20,4,5),matrix(1:5,1,5),f=sum)
 [,1] [,2] [,3] [,4]
[1,]   60   65   70   75

Cheers,

Berwin

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply and plot

2005-10-13 Thread Luis Ridao Cruz
R-help,

I use the code below to plot some data by applying apply function.
But I don't know how I can get the argument type or col on the
plot function to distinguish the different lines
in the graph:

apply ( my.data, 2, function ( x ) lines ( dimnames ( my.data ) [[1]] ,
x ) )


Thank you in advance

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply and plot

2005-10-13 Thread Marc Schwartz (via MN)
On Thu, 2005-10-13 at 14:50 +0100, Luis Ridao Cruz wrote:
 R-help,
 
 I use the code below to plot some data by applying apply function.
 But I don't know how I can get the argument type or col on the
 plot function to distinguish the different lines
 in the graph:
 
 apply ( my.data, 2, function ( x ) lines ( dimnames ( my.data ) [[1]] ,
 x ) )
 
 
 Thank you in advance


Rather than trying the use the construct above, take a look at ?matlines
and/or ?matplot (both on the same help page with ?matpoints.)

I think that you will find these purposefully designed functions better
suited to what I believe you are trying to do here.

HTH,

Marc Schwartz

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] Apply a function for each Row

2005-09-14 Thread Marc Bernard
Dear All,
 
I wonder how to apply a given function to  each row of a data frame. I've seen 
this function before  but don't remember its name
 
Thank you,
 
Bernard 


-


[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] Apply a function for each Row

2005-09-14 Thread Barry Rowlingson
Marc Bernard wrote:
 Dear All,
  
 I wonder how to apply a given function to  each row of a data frame. I've 
 seen this function before  but don't remember its name

  You've just said it twice!

'apply'!

Baz

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] Apply a function for each Row

2005-09-14 Thread Liaw, Andy
 From: Barry Rowlingson
 
 Marc Bernard wrote:
  Dear All,
   
  I wonder how to apply a given function to  each row of a 
 data frame. I've seen this function before  but don't 
 remember its name
 
   You've just said it twice!
 
 'apply'!

A small catch:  Marc wants to apply the function to rows of a data frame,
but apply() expects a matrix or array, and will coerce to such if given a
data frame, which may (or may not) be problematic...

Andy

 Baz
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! 
 http://www.R-project.org/posting-guide.html
 
 


__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply and arrays

2005-07-25 Thread Prof Brian Ripley
On Mon, 25 Jul 2005, Uwe Ligges wrote:

 Laura Holt wrote:

 Hi R!

 I have a 3 dimensional array, which is 21 x 3 x 3

 I want to use apply to sum on each 21x3 matrix, which is fine.

 Is there a way that I can do this in 1 step instead of a loop (3), please?

 Don't know which direction you mean, I guess one of the following:

 apply(x, c(1,2), sum)
 apply(x, 3, sum)

Or use rowSums or colSums for clarity (and speed, irrelevant here).

-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply and arrays

2005-07-24 Thread Laura Holt
Hi R!

I have a 3 dimensional array, which is 21 x 3 x 3

I want to use apply to sum on each 21x3 matrix, which is fine.

Is there a way that I can do this in 1 step instead of a loop (3), please?

thanks,
Laura Holt
mailto: [EMAIL PROTECTED]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply the function factor to multiple columns

2005-05-31 Thread Bliese, Paul D LTC USAMH
I have a case where I would like to change multiple columns containing
numbers to factors.  I can change each column one at a time as in:

 

TEMP.FACT$EXPOS01-factor(TEMP.FACT$EXPOS01,levels=c(1,2,3),labels=c(No
ne,Low Impact,MedHigh Imp))

TEMP.FACT$EXPOS02-factor(TEMP.FACT$EXPOS02,levels=c(1,2,3),labels=c(No
ne,Low Impact,MedHigh Imp))

TEMP.FACT$EXPOS03-factor(TEMP.FACT$EXPOS03,levels=c(1,2,3),labels=c(No
ne,Low Impact,MedHigh Imp))

 

 summary(TEMP.FACT[,1:3])

EXPOS01   EXPOS02   EXPOS03   

 None   :219   None   :432   None   :377  

 Low Impact :428   Low Impact :248   Low Impact :297  

 MedHigh Imp:108   MedHigh Imp: 77   MedHigh Imp: 83  

 NA's   : 25   NA's   : 23   NA's   : 23

 

It would be much easier, however to use apply as in:

 

TEMP.FACT [,1:3]-apply(TEMP.FACT[,1:3],2,factor,labels=c(None,Low
Impact,MedHigh Imp))

 

This appears to work (no error messages); however, this does not
actually change the variables to factors.  That is they are still
treated as numbers:

 

 summary(TEMP.FACT[,1:3])

EXPOS01  EXPOS02  EXPOS03  

 Min.   : 1.000   Min.   : 1.000   Min.   : 1.000  

 1st Qu.: 1.000   1st Qu.: 1.000   1st Qu.: 1.000  

 Median : 2.000   Median : 1.000   Median : 2.000  

 Mean   : 1.853   Mean   : 1.531   Mean   : 1.612  

 3rd Qu.: 2.000   3rd Qu.: 2.000   3rd Qu.: 2.000  

 Max.   : 3.000   Max.   : 3.000   Max.   : 3.000  

 NA's   :25.000   NA's   :23.000   NA's   :23.000

 

Any ideas on how I could efficiently change a lot of columns to factors?

 

Thanks,

 

PB

 


[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply the function factor to multiple columns

2005-05-31 Thread Dimitris Rizopoulos

you could try this way:

dat - data.frame(V1 = factor(1:3), V2 = factor(1:3), V3 = 
factor(1:3))
dat[1:3] - lapply(dat[1:3], factor, labels = c(None, Low Impact, 
MedHigh Imp))

dat


I hope it helps.

Best,
Dimitris


Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


- Original Message - 
From: Bliese, Paul D LTC USAMH [EMAIL PROTECTED]

To: r-help@stat.math.ethz.ch
Sent: Tuesday, May 31, 2005 10:42 AM
Subject: [R] apply the function factor to multiple columns


I have a case where I would like to change multiple columns 
containing

numbers to factors.  I can change each column one at a time as in:



TEMP.FACT$EXPOS01-factor(TEMP.FACT$EXPOS01,levels=c(1,2,3),labels=c(No
ne,Low Impact,MedHigh Imp))

TEMP.FACT$EXPOS02-factor(TEMP.FACT$EXPOS02,levels=c(1,2,3),labels=c(No
ne,Low Impact,MedHigh Imp))

TEMP.FACT$EXPOS03-factor(TEMP.FACT$EXPOS03,levels=c(1,2,3),labels=c(No
ne,Low Impact,MedHigh Imp))




summary(TEMP.FACT[,1:3])


   EXPOS01   EXPOS02   EXPOS03

None   :219   None   :432   None   :377

Low Impact :428   Low Impact :248   Low Impact :297

MedHigh Imp:108   MedHigh Imp: 77   MedHigh Imp: 83

NA's   : 25   NA's   : 23   NA's   : 23



It would be much easier, however to use apply as in:



TEMP.FACT 
[,1:3]-apply(TEMP.FACT[,1:3],2,factor,labels=c(None,Low

Impact,MedHigh Imp))



This appears to work (no error messages); however, this does not
actually change the variables to factors.  That is they are still
treated as numbers:




summary(TEMP.FACT[,1:3])


   EXPOS01  EXPOS02  EXPOS03

Min.   : 1.000   Min.   : 1.000   Min.   : 1.000

1st Qu.: 1.000   1st Qu.: 1.000   1st Qu.: 1.000

Median : 2.000   Median : 1.000   Median : 2.000

Mean   : 1.853   Mean   : 1.531   Mean   : 1.612

3rd Qu.: 2.000   3rd Qu.: 2.000   3rd Qu.: 2.000

Max.   : 3.000   Max.   : 3.000   Max.   : 3.000

NA's   :25.000   NA's   :23.000   NA's   :23.000



Any ideas on how I could efficiently change a lot of columns to 
factors?




Thanks,



PB




[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! 
http://www.R-project.org/posting-guide.html




__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply question

2005-05-02 Thread Christoph Scherber
Dear R users,
I´ve got a simple question but somehow I can´t find the solution:
I have a data frame with columns 1-5 containing one set of integer 
values, and columns 6-10 containing another set of integer values. 
Columns 6-10 contain NA´s at some places.

I now want to calculate
(1) the number of values in each row of columns 6-10 that were NA´s
(2) the sum of all values on columns 1-5 for which there were no missing 
values in the corresponding cells of columns 6-10.

Example: (let´s call the data frame data)
Col1   Col2   Col3   Col4   Col5   Col6   Col7   Col8   Col9   Col10
1  2  5  2  3  NA  5  NA1  4
3  1  4  5  2  6  NA  4 NA 1
The result would then be (for the first row)
(1) There were 2 NA´s in columns 6-10.
(2) The mean of Columns 1-5 was 2+2+3=7 (because there were NA´s in the 
1st and 3rd position in rows 6-10)

So far, I know how to calculate the rowSums for the data.frame, but I 
don´t know how to condition these on the values of columns 6-10

rowSums(data[,1:5]) #that´s straightforward
apply(data[,6:19],1,function(x)sum(is.na(x))) #this also works fine
But I don´t know how to select just the desired values of columns 1-5 
(as described above)

Can anyone help me? Thanks a lot in advance!
Best regards
Christoph
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply question

2005-05-02 Thread Sean Davis
- Original Message - 
From: Christoph Scherber [EMAIL PROTECTED]
To: r-help@stat.math.ethz.ch
Sent: Monday, May 02, 2005 10:52 AM
Subject: [R] apply question


Dear R users,
I´ve got a simple question but somehow I can´t find the solution:
I have a data frame with columns 1-5 containing one set of integer values, 
and columns 6-10 containing another set of integer values. Columns 6-10 
contain NA´s at some places.

I now want to calculate
(1) the number of values in each row of columns 6-10 that were NA´s
(2) the sum of all values on columns 1-5 for which there were no missing 
values in the corresponding cells of columns 6-10.

Example: (let´s call the data frame data)
Col1   Col2   Col3   Col4   Col5   Col6   Col7   Col8   Col9   Col10
1  2  5  2  3  NA  5  NA1  4
3  1  4  5  2  6  NA  4 NA 1
The result would then be (for the first row)
(1) There were 2 NA´s in columns 6-10.
(2) The mean of Columns 1-5 was 2+2+3=7 (because there were NA´s in the 
1st and 3rd position in rows 6-10)

So far, I know how to calculate the rowSums for the data.frame, but I 
don´t know how to condition these on the values of columns 6-10

rowSums(data[,1:5]) #that´s straightforward
apply(data[,6:19],1,function(x)sum(is.na(x))) #this also works fine
But I don´t know how to select just the desired values of columns 1-5 (as 
described above)
tmp - rowSums(data[apply(data[,6:19],1,function(x) sum(is.na(x)))==0,1:5])
Now, tmp contains only the rowsums for the rows with no NAs in the other 
columns.

Sean
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


RE: [R] apply question

2005-05-02 Thread Liaw, Andy
Try:

 ## Number of NAs in columns 6-10.
 colSums(is.na(data[6:10]))
 Col6  Col7  Col8  Col9 Col10 
1 1 1 1 0 
 
 ## Number of NAs in each row of columns 6-10.
 rowSums(is.na(data[6:10]))
1 2 
2 2 
 
 ## Sums of rows 1-5 omitting corresponding NAs in cols 6-10.
 rowSums(data[,1:5] * !is.na(data[,6:10]))
1 2 
7 9 

If all entries are numeric, it'd be easier to use matrices instead of data
frames.

HTH,
Andy

 From: Christoph Scherber
 
 Dear R users,
 
 I´ve got a simple question but somehow I can´t find the solution:
 
 I have a data frame with columns 1-5 containing one set of integer 
 values, and columns 6-10 containing another set of integer values. 
 Columns 6-10 contain NA´s at some places.
 
 I now want to calculate
 (1) the number of values in each row of columns 6-10 that were NA´s
 (2) the sum of all values on columns 1-5 for which there were 
 no missing 
 values in the corresponding cells of columns 6-10.
 
 
 Example: (let´s call the data frame data)
 
 Col1   Col2   Col3   Col4   Col5   Col6   Col7   Col8   Col9   Col10
 1  2  5  2  3  NA  5  NA1  4
 3  1  4  5  2  6  NA  4 NA 1
 
 The result would then be (for the first row)
 (1) There were 2 NA´s in columns 6-10.
 (2) The mean of Columns 1-5 was 2+2+3=7 (because there were 
 NA´s in the 
 1st and 3rd position in rows 6-10)
 
 So far, I know how to calculate the rowSums for the data.frame, but I 
 don´t know how to condition these on the values of columns 6-10
 
 rowSums(data[,1:5]) #that´s straightforward
 apply(data[,6:19],1,function(x)sum(is.na(x))) #this also works fine
 
 But I don´t know how to select just the desired values of columns 1-5 
 (as described above)
 
 
 Can anyone help me? Thanks a lot in advance!
 
 Best regards
 Christoph
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! 
 http://www.R-project.org/posting-guide.html
 
 


__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply question

2005-05-02 Thread Dimitris Rizopoulos
you could try something like this:
dat - rbind(c(1, 2, 5, 2, 3, NA, 5, NA, 1, 4),
c(3, 1, 4, 5, 2, 6, NA, 4, NA, 1))
##
# (1)
rowSums(is.na(dat[, 6:10]))
## (2)
dat. - dat[, 1:5]
dat.[is.na(dat[, 6:10])] - NA
rowSums(dat., na.rm=TRUE)
rowMeans(dat., na.rm=TRUE)
I hope it helps.
Best,
Dimitris

Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
- Original Message - 
From: Christoph Scherber [EMAIL PROTECTED]
To: r-help@stat.math.ethz.ch
Sent: Monday, May 02, 2005 4:52 PM
Subject: [R] apply question


Dear R users,
I´ve got a simple question but somehow I can´t find the solution:
I have a data frame with columns 1-5 containing one set of integer 
values, and columns 6-10 containing another set of integer values. 
Columns 6-10 contain NA´s at some places.

I now want to calculate
(1) the number of values in each row of columns 6-10 that were NA´s
(2) the sum of all values on columns 1-5 for which there were no 
missing values in the corresponding cells of columns 6-10.

Example: (let´s call the data frame data)
Col1   Col2   Col3   Col4   Col5   Col6   Col7   Col8   Col9   Col10
1  2  5  2  3  NA  5  NA1  4
3  1  4  5  2  6  NA  4 NA 1
The result would then be (for the first row)
(1) There were 2 NA´s in columns 6-10.
(2) The mean of Columns 1-5 was 2+2+3=7 (because there were NA´s in 
the 1st and 3rd position in rows 6-10)

So far, I know how to calculate the rowSums for the data.frame, but 
I don´t know how to condition these on the values of columns 6-10

rowSums(data[,1:5]) #that´s straightforward
apply(data[,6:19],1,function(x)sum(is.na(x))) #this also works fine
But I don´t know how to select just the desired values of columns 
1-5 (as described above)

Can anyone help me? Thanks a lot in advance!
Best regards
Christoph
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! 
http://www.R-project.org/posting-guide.html

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply question

2005-05-02 Thread Gabor Grothendieck
On 5/2/05, Christoph Scherber [EMAIL PROTECTED] wrote:
 Dear R users,
 
 I´ve got a simple question but somehow I can´t find the solution:
 
 I have a data frame with columns 1-5 containing one set of integer
 values, and columns 6-10 containing another set of integer values.
 Columns 6-10 contain NA´s at some places.
 
 I now want to calculate
 (1) the number of values in each row of columns 6-10 that were NA´s

Supposing our data is called DF,

rowSums(!is.na(DF[,6:10]))

 (2) the sum of all values on columns 1-5 for which there were no missing
 values in the corresponding cells of columns 6-10.

In the expression below 1 + 0 *DF[,6:10] is like DF[,6:10] except
all non-NAs are replaced by 1.  Multiplying DF[,1:5] by that
effectively replaces each element in DF[,1:5] with an NA if
the corresponding DF[,6:10] contained an NA.

rowSums( DF[,1:5] * (1 + 0 * DF[,6:10]), na.rm = TRUE )

 
 Example: (let´s call the data frame data)
 
 Col1   Col2   Col3   Col4   Col5   Col6   Col7   Col8   Col9   Col10
 1  2  5  2  3  NA  5  NA1  4
 3  1  4  5  2  6  NA  4 NA 1
 
 The result would then be (for the first row)
 (1) There were 2 NA´s in columns 6-10.
 (2) The mean of Columns 1-5 was 2+2+3=7 (because there were NA´s in the
 1st and 3rd position in rows 6-10)

I guess you meant sum when you referred to mean in (2).  If you really
do want the mean replace rowSums with rowMeans in the expression
given above in the answer to (2).

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply vs sapply vs loop - lm() call appl(y)ied on array

2005-04-21 Thread Christoph Lehmann
Dear useRs
(Code of the now mentioned small example is below)
I have 7 * 8 * 9 = 504 series of data (each length 5). For each of 
theses series I want to compute a lm(), where the designmatrx X is the 
same for all these computations.

The 504 series are in an array of dimension d.dim - c(5, 7, 8, 9)
means, the first dimension holds the data-series.
The lm computation needs performance optimization, since in fact the 
dimensions are much larger. I compared the following approaches:

using a for-loop. using apply, and using sapply. All of these require 
roughly the same time of computation. I was astonished since I expected 
at least sapply to outperfomr the for-loop.

Do you have me another solution, which is faster? many thanks
here is the code
## --
t.length - 5
d.dim - c(t.length,7,8,9) # dimesions: time, x, y, z
Y - array( rep(1:t.length, prod(d.dim)) + rnorm(prod(d.dim), 0, 0.1), 
d.dim)
X - c(1,3,2,4,5)

##  performance tests
## using for loop
date()
z - rep(0, prod(d.dim[2:4]))
l - 0
for (i in 1:dim(Y)[4])
 for (j in 1:dim(Y)[3])
  for (k in 1:dim(Y)[2]) {
l - l + 1
z[l] - unlist(summary(lm(Y[,k, j, i] ~ X)))$r.squared
  }
date()
## using apply
date()
z - apply(Y, 2:4, function(x) unlist(summary(lm(x ~ X)))$r.squared)
date()
## using sapply
date()
fac - rep(1:prod(d.dim[2:4]), rep(t.length, prod(d.dim[2:4])))
z - sapply(split(as.vector(Y), fac), FUN = function(x) 
unlist(summary(lm(x ~ X)))$r.squared)
dim(z) - d.dim[2:4]
date()

## --
--
Christoph LehmannPhone:  ++41 31 930 93 83
Department of Psychiatric NeurophysiologyMobile: ++41 76 570 28 00
University Hospital of Clinical Psychiatry   Fax:++41 31 930 99 61
Waldau[EMAIL PROTECTED]
CH-3000 Bern 60 http://www.puk.unibe.ch/cl/pn_ni_cv_cl_04.html
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


RE: [R] apply vs sapply vs loop - lm() call appl(y)ied on array

2005-04-21 Thread Wiener, Matthew
Christoph --

There was just a thread on this earlier this week.  You can search in the
archives for the title:   refitting lm() with same x, different y.

(Actually, it doesn't turn up in the R site search yet, at least for me.
But if you just go to the archive of recent messages, available through
CRAN, you can search on refitting and find it.  The original post was from
William Valdar, on April 19.)

Hope this helps,

Matt Wiener

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Christoph Lehmann
Sent: Thursday, April 21, 2005 9:24 AM
To: R-help@stat.math.ethz.ch
Subject: [R] apply vs sapply vs loop - lm() call appl(y)ied on array


Dear useRs

(Code of the now mentioned small example is below)

I have 7 * 8 * 9 = 504 series of data (each length 5). For each of 
theses series I want to compute a lm(), where the designmatrx X is the 
same for all these computations.

The 504 series are in an array of dimension d.dim - c(5, 7, 8, 9)
means, the first dimension holds the data-series.

The lm computation needs performance optimization, since in fact the 
dimensions are much larger. I compared the following approaches:

using a for-loop. using apply, and using sapply. All of these require 
roughly the same time of computation. I was astonished since I expected 
at least sapply to outperfomr the for-loop.

Do you have me another solution, which is faster? many thanks

here is the code
## --
t.length - 5
d.dim - c(t.length,7,8,9) # dimesions: time, x, y, z
Y - array( rep(1:t.length, prod(d.dim)) + rnorm(prod(d.dim), 0, 0.1), 
d.dim)
X - c(1,3,2,4,5)

##  performance tests
## using for loop
date()
z - rep(0, prod(d.dim[2:4]))
l - 0
for (i in 1:dim(Y)[4])
  for (j in 1:dim(Y)[3])
   for (k in 1:dim(Y)[2]) {
 l - l + 1
 z[l] - unlist(summary(lm(Y[,k, j, i] ~ X)))$r.squared
   }
date()

## using apply
date()
z - apply(Y, 2:4, function(x) unlist(summary(lm(x ~ X)))$r.squared)
date()

## using sapply
date()
fac - rep(1:prod(d.dim[2:4]), rep(t.length, prod(d.dim[2:4])))
z - sapply(split(as.vector(Y), fac), FUN = function(x) 
unlist(summary(lm(x ~ X)))$r.squared)
dim(z) - d.dim[2:4]
date()

## --

-- 
Christoph LehmannPhone:  ++41 31 930 93 83
Department of Psychiatric NeurophysiologyMobile: ++41 76 570 28 00
University Hospital of Clinical Psychiatry   Fax:++41 31 930 99 61
Waldau[EMAIL PROTECTED]
CH-3000 Bern 60 http://www.puk.unibe.ch/cl/pn_ni_cv_cl_04.html

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply vs sapply vs loop - lm() call appl(y)ied on array

2005-04-21 Thread Christoph Lehmann
Ok thanks to a hint of Matthew to a former post with a similar request I 
have now three faster solutions (see below), the last one being the 
fastest, but the former two also faster than the for-loop, 
apply(lm(formula)) and sapply(lm(formula)) versions in my last mail:

one problem only: using lsfit I can't get directly measures such as 
r.squared ...

---
## using lm with a matrix response (recommended by BDR)
date()
rsq -unlist(summary(lm(array(c(Y), dim = c(t.length, prod(d.dim[2:4]))) 
~ X)))[seq(22, prod(d.dim[2:4]) * 30, by = 30)] #get r.squared list-element
names(rsq) - prod(d.dim[2:4])
rsq - array(rsq, dim = d.dim[2:4])
date()

## using sapply and lsfit instead of lm (recommended by Kevin Wright)
date()
fac - rep(1:prod(d.dim[2:4]), rep(t.length, prod(d.dim[2:4])))
z - sapply(split(as.vector(Y), fac), FUN = function(x) lsfit(X, x)$coef[2])
dim(z) - d.dim[2:4]
date()
## using lsfit with a matrix response:
date()
rsq -lsfit(X, array(c(Y), dim = c(t.length, prod(d.dim[2:4]$coef[2,]
names(rsq) - prod(d.dim[2:4])
rsq - array(rsq, dim = d.dim[2:4])
date()
--
thanks
Christoph
Wiener, Matthew wrote:
Christoph --
There was just a thread on this earlier this week.  You can search in the
archives for the title:   refitting lm() with same x, different y.
(Actually, it doesn't turn up in the R site search yet, at least for me.
But if you just go to the archive of recent messages, available through
CRAN, you can search on refitting and find it.  The original post was from
William Valdar, on April 19.)
Hope this helps,
Matt Wiener
-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Christoph Lehmann
Sent: Thursday, April 21, 2005 9:24 AM
To: R-help@stat.math.ethz.ch
Subject: [R] apply vs sapply vs loop - lm() call appl(y)ied on array
Dear useRs
(Code of the now mentioned small example is below)
I have 7 * 8 * 9 = 504 series of data (each length 5). For each of 
theses series I want to compute a lm(), where the designmatrx X is the 
same for all these computations.

The 504 series are in an array of dimension d.dim - c(5, 7, 8, 9)
means, the first dimension holds the data-series.
The lm computation needs performance optimization, since in fact the 
dimensions are much larger. I compared the following approaches:

using a for-loop. using apply, and using sapply. All of these require 
roughly the same time of computation. I was astonished since I expected 
at least sapply to outperfomr the for-loop.

Do you have me another solution, which is faster? many thanks
here is the code
## --
t.length - 5
d.dim - c(t.length,7,8,9) # dimesions: time, x, y, z
Y - array( rep(1:t.length, prod(d.dim)) + rnorm(prod(d.dim), 0, 0.1), 
d.dim)
X - c(1,3,2,4,5)

##  performance tests
## using for loop
date()
z - rep(0, prod(d.dim[2:4]))
l - 0
for (i in 1:dim(Y)[4])
  for (j in 1:dim(Y)[3])
   for (k in 1:dim(Y)[2]) {
 l - l + 1
 z[l] - unlist(summary(lm(Y[,k, j, i] ~ X)))$r.squared
   }
date()
## using apply
date()
z - apply(Y, 2:4, function(x) unlist(summary(lm(x ~ X)))$r.squared)
date()
## using sapply
date()
fac - rep(1:prod(d.dim[2:4]), rep(t.length, prod(d.dim[2:4])))
z - sapply(split(as.vector(Y), fac), FUN = function(x) 
unlist(summary(lm(x ~ X)))$r.squared)
dim(z) - d.dim[2:4]
date()

## --
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply

2005-04-07 Thread malte
Hi,
simple question I guess:
the following line works well:
aveBehav=c(apply(sdata, 2, mean))
However, I would like to pass an argument to the function mean, namely 
na.rm=TRUE

Does anyone knows how to do this?
Thanks in advance,
Jan
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply

2005-04-07 Thread Sean Davis
On Apr 7, 2005, at 8:27 AM, malte wrote:
Hi,
simple question I guess:
the following line works well:
aveBehav=c(apply(sdata, 2, mean))
However, I would like to pass an argument to the function mean, namely 
na.rm=TRUE

apply(sdata,2,function(x) {mean(x,na.rm=TRUE)})
Sean
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


RE: [R] apply

2005-04-07 Thread Liaw, Andy
 From: malte
 
 Hi,
 
 simple question I guess:
 
 the following line works well:
 
 aveBehav=c(apply(sdata, 2, mean))
 
 However, I would like to pass an argument to the function 
 mean, namely 
 na.rm=TRUE
 
 Does anyone knows how to do this?

aveBehav - apply(sdata, 2, mean, na.rm=TRUE)

or more efficiently:

aveBehav - colMeans(sdata, na.rm=TRUE)

Read ?apply and look at the ... argument.  If you don't understand how it
works, try the example on that page.

Andy
 
 Thanks in advance,
 
 Jan
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! 
 http://www.R-project.org/posting-guide.html
 
 


__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply

2005-04-07 Thread Dimitris Rizopoulos
try,
apply(sdata, 2, mean, na.rm=TRUE)
or
# assuming `sdata' is a matrix
colMeans(sdata, na.rm=TRUE)
I hope it helps.
Best,
Dimitris

Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
- Original Message - 
From: malte [EMAIL PROTECTED]
To: r-help@stat.math.ethz.ch
Sent: Thursday, April 07, 2005 2:27 PM
Subject: [R] apply


Hi,
simple question I guess:
the following line works well:
aveBehav=c(apply(sdata, 2, mean))
However, I would like to pass an argument to the function mean, 
namely na.rm=TRUE

Does anyone knows how to do this?
Thanks in advance,
Jan
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! 
http://www.R-project.org/posting-guide.html

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply

2005-04-07 Thread Seth Falcon
malte [EMAIL PROTECTED] writes:
 aveBehav=c(apply(sdata, 2, mean))

aveBehav= apply(sdata, 2, mean, na.rm=TRUE)

and

?apply will tell you about this.

+ seth

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply

2005-04-07 Thread Petr Pikal
On 7 Apr 2005 at 14:27, malte wrote:

 Hi,
 
 simple question I guess:
 
 the following line works well:
 
 aveBehav=c(apply(sdata, 2, mean))

Hallo
 try

aveBehav=c(apply(sdata, 2, mean, na.rm=T))

Cheers
Petr


 
 However, I would like to pass an argument to the function mean,
 namely na.rm=TRUE
 
 Does anyone knows how to do this?
 
 Thanks in advance,
 
 Jan
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide!
 http://www.R-project.org/posting-guide.html
Petr Pikal
[EMAIL PROTECTED]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply a function to a rolling subset of a vector

2005-03-02 Thread Whit Armstrong
Does anyone know an easy way to calculate the rolling 20 period average
or sum of a vector?

For instance:
x - rnorm(1000)

y - apply.subset(x,20,fun=sum)

The first element of y would contain the sum of elements 1 to 20, the
second element of y 
would contain the sum of elements 2:21, and so on.

I thought I had seen this on the list a year or so ago, but I couldn't
find anything in the archives.


Thanks in advance,
Whit

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply a function to a rolling subset of a vector

2005-03-02 Thread Ken Knoblauch
Try this:

 ?convolve
 x-rnorm(1000)
 y-rep(1,20)
 z-convolve(x,y,type=filter)
 plot(x,type=l)
 str(z)
 num [1:981] 6.31 7.28 8.16 7.39 4.65 ...
 lines(c(rep(0,10),z,rep(0,10)),col=yellow,lwd=3)
 lines(c(rep(0,10),z,rep(0,10))/length(y),col=red,lwd=3) #running mean

You wrote:
Does anyone know an easy way to calculate the rolling 20 period average
or sum of a vector?

For instance:
x - rnorm(1000)

y - apply.subset(x,20,fun=sum)

The first element of y would contain the sum of elements 1 to 20, the
second element of y 
would contain the sum of elements 2:21, and so on.

I thought I had seen this on the list a year or so ago, but I couldn't
find anything in the archives.


Thanks in advance,
Whit

[[alternative HTML version deleted]]


Ken Knoblauch
Inserm U 371
Cerveau et Vision
18 avenue du Doyen Lepine
69675 Bron cedex
France
tel: +33 (0)4 72 91 34 77
fax: +33 (0)4 72 91 34 61
portable: 06 84 10 64 10

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply a function to a rolling subset of a vector

2005-03-02 Thread Duncan Murdoch
On Wed, 2 Mar 2005 17:22:43 -0500, Whit Armstrong
[EMAIL PROTECTED] wrote :

Does anyone know an easy way to calculate the rolling 20 period average
or sum of a vector?

For instance:
x - rnorm(1000)

y - apply.subset(x,20,fun=sum)

The first element of y would contain the sum of elements 1 to 20, the
second element of y 
would contain the sum of elements 2:21, and so on.

I thought I had seen this on the list a year or so ago, but I couldn't
find anything in the archives.

I don't know of a general purpose function, but filter() (in the stats
package) can do the example you give, or any other linear filter.

e.g.

x - rnorm(1000)
y - filter(x, rep(1,20))

puts 20 element sums into y.  The vector ends up the same length as x,
with NAs at the beginning and end (by default).

Duncan Murdoch

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply a function to a rolling subset of a vector

2005-03-02 Thread Marc Schwartz
On Wed, 2005-03-02 at 17:22 -0500, Whit Armstrong wrote:
 Does anyone know an easy way to calculate the rolling 20 period average
 or sum of a vector?
 
 For instance:
 x - rnorm(1000)
 
 y - apply.subset(x,20,fun=sum)
 
 The first element of y would contain the sum of elements 1 to 20, the
 second element of y 
 would contain the sum of elements 2:21, and so on.
 
 I thought I had seen this on the list a year or so ago, but I couldn't
 find anything in the archives.

You can use the running() function in the gtools package, which is in
the gregmisc bundle:

x - rnorm(1000)

 running(x, fun = sum, width = 20)
 1:20  2:21  3:22  4:23  5:24
 -2.009684610  -2.205737077  -1.410810606  -2.226661837  -1.684604289
 6:25  7:26  8:27  9:28 10:29
 -4.492008605  -3.816273719  -5.348364598  -6.444591766  -5.263013812
11:30 12:31 13:32 14:33 15:34
 -4.609829115  -5.935537291  -6.909232329  -4.881021777  -5.803659103
...

See ?running for more information, after installing gregmisc from CRAN. 

HTH,

Marc Schwartz

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply a function to a rolling subset of a vector

2005-03-02 Thread Gabor Grothendieck
Whit Armstrong whit at twinfieldscapital.com writes:

: 
: Does anyone know an easy way to calculate the rolling 20 period average
: or sum of a vector?
: 
: For instance:
: x - rnorm(1000)
: 
: y - apply.subset(x,20,fun=sum)
: 
: The first element of y would contain the sum of elements 1 to 20, the
: second element of y 
: would contain the sum of elements 2:21, and so on.
: 
: I thought I had seen this on the list a year or so ago, but I couldn't
: find anything in the archives.
: 

Look at ?filter .  Also ?embed and gtools::running .  filter is the
fastest.

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


RE: [R] apply a function to a rolling subset of a vector

2005-03-02 Thread Whit Armstrong
Thanks, everyone, for all the suggestions.

The rollFun turs out to be just what I needed.

Cheers,
Whit
 

-Original Message-
From: Kjetil Brinchmann Halvorsen [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, March 02, 2005 5:45 PM
To: Whit Armstrong
Cc: r-help@stat.math.ethz.ch
Subject: Re: [R] apply a function to a rolling subset of a vector

Whit Armstrong wrote:

Does anyone know an easy way to calculate the rolling 20 period average

or sum of a vector?

For instance:
x - rnorm(1000)

y - apply.subset(x,20,fun=sum)
  

help.search(rolling)

gives me (among others)

RollingAnalysis(fSeries)
Rolling Analysis

so trying

library(fSeries)
x - rnorm(1000)
y - rollFun(x, 20, mean)

Kjetil

The first element of y would contain the sum of elements 1 to 20, the 
second element of y would contain the sum of elements 2:21, and so on.

I thought I had seen this on the list a year or so ago, but I couldn't 
find anything in the archives.


Thanks in advance,
Whit

   [[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! 
http://www.R-project.org/posting-guide.html



  



-- 

Kjetil Halvorsen.

Peace is the most effective weapon of mass construction.
   --  Mahdi Elmandjra




--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


RE: [R] apply for nested lists

2005-01-26 Thread Berton Gunter
apply() statements **are** disguised loops and therefore are **not**
necessarily more efficient than explicit looping. Their principal advantage
is usually code readability. 

As another readability issue, note that x[[i]][[j]][[k]] can be abbreviated
to x[[c(i,j,k]].

I leave to others the pleasure of deciphering and improving your code,
however. 

-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
 
The business of the statistician is to catalyze the scientific learning
process.  - George E. P. Box
 
 

 -Original Message-
 From: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] On Behalf Of 
 Alexandre Sanchez Pla
 Sent: Wednesday, January 26, 2005 8:50 AM
 To: r-help@stat.math.ethz.ch
 Subject: [R] apply for nested lists
 
 Hi,
 
 I am working with lists whose terms are lists whose terms are 
 lists. Although 
 the real ones contain locuslink identifiers and GO 
 annotations (I work with the 
 Bioconductor GO) package, I have prepared an simplified 
 example of what I have 
 and what I would like to do with it:
 
 Imagine I have a list such as:
 
 tst.list-list(1=list(1A=list(ID=1A,VAL=172),1B=list
 (ID=1B,VAL=134),1C=list(ID=1C,VAL=0)),2=list(2A=NA),
 3=list(3A=list
 (ID=3A,VAL=33),3B=list(ID=3B,VAL=2)))
 
 I would like, for instance, to be able to extract some values 
 such as the 
 content of the VAL field, which may sometimes not be available.
 I may do it using a nested for such as:
 
 x-character(0)
 for (i in 1:length(tst.list)){
 if (!is.na(tst.list[[i]][[1]][[1]])){
 for (j in 1:length(tst.list[[i]]))
 {x-c(x,tst.list[[i]][[j]]$VAL)}}
 else
 {x-c(x, NA)}}
 
 which gives me what I need
 
  x
 [1] 172 134 0   NA  33  2  
 
 According to most R documents this may be done more 
 efficiently using apply 
 instructions, but I have failed in my temptatives to obtain the same
 
 Thanks for any help.
 
 Alex
 
 
 -- 
 Dr.Alex Sánchez
 Departament d'Estadística.
 Universitat de Barcelona
 [EMAIL PROTECTED]
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! 
 http://www.R-project.org/posting-guide.html


__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply for nested lists

2005-01-26 Thread Gabor Grothendieck
Alexandre Sanchez Pla asanchez at ub.edu writes:

: 
: Hi,
: 
: I am working with lists whose terms are lists whose terms are lists. 
Although 
: the real ones contain locuslink identifiers and GO annotations (I work with 
the 
: Bioconductor GO) package, I have prepared an simplified example of what I 
have 
: and what I would like to do with it:
: 
: Imagine I have a list such as:
: 
: tst.list-list(1=list(1A=list(ID=1A,VAL=172),1B=list
: (ID=1B,VAL=134),1C=list(ID=1C,VAL=0)),2=list(2A=NA),3=list
(3A=list
: (ID=3A,VAL=33),3B=list(ID=3B,VAL=2)))
: 
: I would like, for instance, to be able to extract some values such as the 
: content of the VAL field, which may sometimes not be available.
: I may do it using a nested for such as:
: 
: x-character(0)
: for (i in 1:length(tst.list)){
: if (!is.na(tst.list[[i]][[1]][[1]])){
: for (j in 1:length(tst.list[[i]]))
: {x-c(x,tst.list[[i]][[j]]$VAL)}}
: else
: {x-c(x, NA)}}
: 
: which gives me what I need
: 
:  x
: [1] 172 134 0   NA  33  2  
: 
: According to most R documents this may be done more efficiently using apply 
: instructions, but I have failed in my temptatives to obtain the same
: 
: Thanks for any help.
: 

Try this:

f - function(x) if (is.null(x$VAL)) NA else x$VAL
unlist(lapply(tst.list, lapply, f))

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply for nested lists

2005-01-26 Thread Kevin Bartz
Actually, what you want is sapply.
sapply(tst.list, [[, VAL)
Kevin
Alexandre Sanchez Pla wrote:
Hi,
I am working with lists whose terms are lists whose terms are lists. Although 
the real ones contain locuslink identifiers and GO annotations (I work with the 
Bioconductor GO) package, I have prepared an simplified example of what I have 
and what I would like to do with it:

Imagine I have a list such as:
tst.list-list(1=list(1A=list(ID=1A,VAL=172),1B=list
(ID=1B,VAL=134),1C=list(ID=1C,VAL=0)),2=list(2A=NA),3=list(3A=list
(ID=3A,VAL=33),3B=list(ID=3B,VAL=2)))
I would like, for instance, to be able to extract some values such as the 
content of the VAL field, which may sometimes not be available.
I may do it using a nested for such as:

x-character(0)
for (i in 1:length(tst.list)){
if (!is.na(tst.list[[i]][[1]][[1]])){
for (j in 1:length(tst.list[[i]]))
{x-c(x,tst.list[[i]][[j]]$VAL)}}
else
{x-c(x, NA)}}

which gives me what I need

x
[1] 172 134 0   NA  33  2  

According to most R documents this may be done more efficiently using apply 
instructions, but I have failed in my temptatives to obtain the same

Thanks for any help.
Alex

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply function

2004-10-20 Thread Eric Pellegrini
Hi all,

I have a question about apply function. Is that possible to pass some 
non-default arguments in the function we want to apply ?

For example:

if mat is a matrix and I want to use the tabulate function on its row.

The command apply(mat,1,tabulate) works but I have problem with this one 
apply(mat, 1, tabulate(nbins=4)).

Any clue ?

Thanks,

Eric



-- 
Eric Pellegrini, PhD
Computer-Chemie-Centrum
University of Erlangen-Nürnberg
Nägelbachstraße, 25
D-91052 Erlangen
Germany

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


RE: [R] apply function

2004-10-20 Thread Liaw, Andy
Try apply(mat, 1, tabulate, nbins=4).

HTH,
Andy

 From: Eric Pellegrini
 
 Hi all,
 
 I have a question about apply function. Is that possible to pass some 
 non-default arguments in the function we want to apply ?
 
 For example:
 
 if mat is a matrix and I want to use the tabulate 
 function on its row.
 
 The command apply(mat,1,tabulate) works but I have problem 
 with this one 
 apply(mat, 1, tabulate(nbins=4)).
 
 Any clue ?
 
 Thanks,
 
   Eric
 
 
 
 -- 
 Eric Pellegrini, PhD
 Computer-Chemie-Centrum
 University of Erlangen-Nürnberg
 Nägelbachstraße, 25
 D-91052 Erlangen
 Germany
 
 __
 [EMAIL PROTECTED] mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! 
 http://www.R-project.org/posting-guide.html
 


__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply function

2004-10-20 Thread Peter Dalgaard
Eric Pellegrini [EMAIL PROTECTED] writes:

 Hi all,
 
 I have a question about apply function. Is that possible to pass some 
 non-default arguments in the function we want to apply ?
 
 For example:
 
 if mat is a matrix and I want to use the tabulate function on its row.
 
 The command apply(mat,1,tabulate) works but I have problem with this one 
 apply(mat, 1, tabulate(nbins=4)).
 
 Any clue ?

You might have gotten one by R'ing TFHP 

 apply(mat, 1, tabulate, nbins=4)

There's even an example!

-- 
   O__   Peter Dalgaard Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics 2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark  Ph: (+45) 35327918
~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply ( , , table)

2004-08-24 Thread White . Denis




a - matrix (c(
7, 1, 1, 2, 6,
3, 4, 0, 1, 4,
5, 1, 8, 4, 4,
6, 1, 1, 2, 5), nrow=4, byrow=TRUE)

b - apply (a, 1, table)

apply documentation says clearly that if the rows of the result of FUN
are the same length, then an array will be returned.  And column-major
would be the appropriate order in R.  But b above is pretty opaque
compared to what one would expect, and what one would get from apply (
, , table) if the rows were not of equal length.  One needs to do
something like

n - matrix (apply (a, 1, function (x) unique (sort (x))), nrow=nrow(a))

to get the corresponding names of b to figure out the counts.

Denis White

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply ( , , table)

2004-08-24 Thread Tony Plate
apply() tries to be a bit smart about what it does (sometimes maybe too 
smart), but it actually is pretty useful a lot of the time.  It's extremely 
widely used, so changing the behavior is not an option -- changing the 
behavior would break a lot of existing code.  (Personally, I'd prefer it if 
apply() put its dimensions back together in a slightly more intelligent 
way, i.e., if apply(x, 1, c) and apply(x, 2, c) returned the same thing, 
but apply is how it is.)

In situations where you don't want apply() to try to construct a matrix 
from your results, you can wrap the results in a list, to force apply() to 
return just a list of results, e.g. (the outer lapply() strips off an 
unnecessary level of list depth):

 b2 - lapply(apply (a, 1, function(x) list(table(x))), [[, 1)
 length(b2)
[1] 4
 b2[[1]]
x
1 2 6 7
2 1 1 1
 attributes(b2[[1]])
$dim
[1] 4
$dimnames
$dimnames$x
[1] 1 2 6 7
$class
[1] table
Your particular case might benefit from more information given to table, 
which allows it to provide results in a more uniform format, e.g.:

 b1 - apply (a, 1, function(x) table(factor(x, levels=0:9)))
 b1
  [,1] [,2] [,3] [,4]
00100
12112
21001
30100
40220
50011
61001
71000
80010
90000

hope this helps,
Tony Plate
At Tuesday 10:42 AM 8/24/2004, [EMAIL PROTECTED] wrote:


a - matrix (c(
7, 1, 1, 2, 6,
3, 4, 0, 1, 4,
5, 1, 8, 4, 4,
6, 1, 1, 2, 5), nrow=4, byrow=TRUE)
b - apply (a, 1, table)
apply documentation says clearly that if the rows of the result of FUN
are the same length, then an array will be returned.  And column-major
would be the appropriate order in R.  But b above is pretty opaque
compared to what one would expect, and what one would get from apply (
, , table) if the rows were not of equal length.  One needs to do
something like
n - matrix (apply (a, 1, function (x) unique (sort (x))), nrow=nrow(a))
to get the corresponding names of b to figure out the counts.
Denis White
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply on a data frame

2004-08-20 Thread Laura Holt
Hi R People:
There are 2 data sets of the iris data: one is iris3, which is a 3-d array, 
and the other is iris, which
is a data frame with 150 rows and 6 variables.

Getting means is straightforward from the 3-day iris3 set:
apply(iris3,c(2,3),mean)
Setosa Versicolor Virginica
Sepal L.  5.006  5.936 6.588
Sepal W.  3.428  2.770 2.974
Petal L.  1.462  4.260 5.552
Petal W.  0.246  1.326 2.026

Is there is similar way to obtain those values from the iris data frame, 
please?

thanks in advance
R Version 1.9.1 Windows.
Sincerely,
Laura Holt
mailto: [EMAIL PROTECTED]
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply and data frames

2004-08-20 Thread Laura Holt
Whoops!
Just found it:
by(iris[,1:4],Species,mean)
Sorry for the inconvenience.
Laura.
Security. http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] apply/looping query

2004-06-16 Thread Laura Quinn
I have several large matrices each having perhaps one or two
data points missing. For instance one point in 2881 is missing. As I want to perform
various analyses on these matrices I feel it is not
unreasonable to linearly interpolate over the missing points. I want to basically 
fill in
the gaps of the original matrix - the following piece of code
doesn't work and i'm not sure where i'm going wrong:

x=1:2881
my.new.matrix-matrix(nrow=2881,ncol=20)
for(i in 1:20){
my.new.matrix[[i]]-approx(x,my.matrix[,i],n=2881)
}

the error message says:

Error: more elements supplied than there are to replace

where am I going wrong??

Thanks in advance..
Laura

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] apply/looping query

2004-06-16 Thread Barry Rowlingson
Laura Quinn wrote:
x=1:2881
my.new.matrix-matrix(nrow=2881,ncol=20)
for(i in 1:20){
my.new.matrix[[i]]-approx(x,my.matrix[,i],n=2881)
}
the error message says:
Error: more elements supplied than there are to replace
where am I going wrong??
approx() returns a list with $x and $y components
I'd use [,x] instead of [[i]] to replace columns.
Hence:
 for(i in 1:20){
  my.new.matrix[,i]-approx(x,my.matrix[,i],n=2881)$y
 }
Baz
__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] Apply a function to each cell of a ragged matrix

2004-02-17 Thread XIAO LIU
R-Helpers:

There are a matrix x and a factor f.  nrow(x) == length(f), e.g.:
x - matrix(1:6, nrow = 3)
f - factor(c(daytime, daytime, night))

I want the sum of all elements of rows of x for each corresponding level in factor 
f,
In this case, I want output like:
daytime [1] x[1,1]+x[2,1]+x[1,2]+x[2,2]
night   [2] x[3,1]+x[3,2]

But, tapply(x,f,sum) or by(x,f,sum) do not work.  What other functions can I use?

Thank you very much

Xiao

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


RE: [R] Apply a function to each cell of a ragged matrix

2004-02-17 Thread Gabor Grothendieck


rowsum(x,f)

---
Date:   Tue, 17 Feb 2004 17:38:46 -0500 
From:   XIAO LIU [EMAIL PROTECTED]
To:   R Help [EMAIL PROTECTED] 
Subject:   [R] Apply a function to each cell of a ragged matrix 

 
R-Helpers:

There are a matrix x and a factor f. nrow(x) == length(f), e.g.:
x - matrix(1:6, nrow = 3)
f - factor(c(daytime, daytime, night))

I want the sum of all elements of rows of x for each corresponding level in factor 
f,
In this case, I want output like:
daytime [1] x[1,1]+x[2,1]+x[1,2]+x[2,2]
night [2] x[3,1]+x[3,2]

But, tapply(x,f,sum) or by(x,f,sum) do not work. What other functions can I use?

Thank you very much

Xiao

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] Apply a function to each cell of a ragged matrix

2004-02-17 Thread Mahmoud K. Okasha
Hi,
you could simply use functions such as:

time - dim (3)
for ( i in 1:3) time [i] - x[i,1]+x[i,2]

the result of time will be the sum of rows.

best regards..

- Original Message -
From: XIAO LIU [EMAIL PROTECTED]
To: R Help [EMAIL PROTECTED]
Sent: Wednesday, February 18, 2004 12:38 AM
Subject: [R] Apply a function to each cell of a ragged matrix


 R-Helpers:

 There are a matrix x and a factor f.  nrow(x) == length(f), e.g.:
 x - matrix(1:6, nrow = 3)
 f - factor(c(daytime, daytime, night))

 I want the sum of all elements of rows of x for each corresponding level
in factor f,
 In this case, I want output like:
 daytime [1] x[1,1]+x[2,1]+x[1,2]+x[2,2]
 night   [2] x[3,1]+x[3,2]

 But, tapply(x,f,sum) or by(x,f,sum) do not work.  What other functions can
I use?

 Thank you very much

 Xiao

 __
 [EMAIL PROTECTED] mailing list
 https://www.stat.math.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html



__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


RE: [R] Apply a function to each cell of a ragged matrix

2004-02-17 Thread Liaw, Andy
 sapply(split(x,f), sum)
daytime   night 
 12   9 

HTH,
Andy

 From: XIAO LIU
 
 R-Helpers:
 
 There are a matrix x and a factor f.  nrow(x) == length(f), e.g.:
 x - matrix(1:6, nrow = 3)
 f - factor(c(daytime, daytime, night))
 
 I want the sum of all elements of rows of x for each 
 corresponding level in factor f,
 In this case, I want output like:
 daytime [1] x[1,1]+x[2,1]+x[1,2]+x[2,2]
 night   [2] x[3,1]+x[3,2]
 
 But, tapply(x,f,sum) or by(x,f,sum) do not work.  What other 
 functions can I use?
 
 Thank you very much
 
 Xiao


--
Notice:  This e-mail message, together with any attachments,...{{dropped}}

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


RE: [R] Apply a function to each cell of a ragged matrix

2004-02-17 Thread Gabor Grothendieck

I misread your post.  Try any of these:

rowSums(rowsum(x,f))

rowsum(rowSums(x),f)

tapply(rowSums(x),f,sum)

by(rowSums(x),f,sum)

---

Date:   Tue, 17 Feb 2004 19:10:11 -0500 (EST) 
From:   Gabor Grothendieck [EMAIL PROTECTED]
To:   [EMAIL PROTECTED], [EMAIL PROTECTED] 
Subject:   RE: [R] Apply a function to each cell of a ragged matrix 

 


rowsum(x,f)

---
Date: Tue, 17 Feb 2004 17:38:46 -0500 
From: XIAO LIU [EMAIL PROTECTED]
To: R Help [EMAIL PROTECTED] 
Subject: [R] Apply a function to each cell of a ragged matrix 


R-Helpers:

There are a matrix x and a factor f. nrow(x) == length(f), e.g.:
x - matrix(1:6, nrow = 3)
f - factor(c(daytime, daytime, night))

I want the sum of all elements of rows of x for each corresponding level in factor 
f,
In this case, I want output like:
daytime [1] x[1,1]+x[2,1]+x[1,2]+x[2,2]
night [2] x[3,1]+x[3,2]

But, tapply(x,f,sum) or by(x,f,sum) do not work. What other functions can I use?

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


RE: [R] apply to multiple arrays simultaneously

2004-01-09 Thread Liaw, Andy
Off the top of my head, seems like you can abind() the two together and then
run apply.  See the abind package on CRAN.

HTH,
Andy

 From: Carlos Soares
 
 Dear R users,
  
 Suppose two arrays which partly have the same dimensions. For 
 instance, 
 a1 and a2 with dim(a1) is c(3,4,5,6) and dim(a2) is 
 c(3,4,7,8). How can 
 I perform an apply on the equivalent part of the dimensions on both 
 arrays simultaneously? Something like:
  
   apply(list(a1, a2), c(1,2), function(x,y) {my.function(x,y)})
  
 A useful bonus would be, assuming that my.function always returns an 
 array withthe same dimensions (e.g., c(2,3,4), that the final result 
 would be an array wit
 h dimensions c(3,4,2,3,4).
  
 With best regards,
 Carlos


--
Notice:  This e-mail message, together with any attachments,...{{dropped}}

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


  1   2   >