Re: [R] Using apply

2018-10-30 Thread Bert Gunter
Indeed. But perhaps it's also worth noting that if such statistics are calculated as implementations of (e.g. anova) formulae still found (sadly) in many statistics texts, then they shouldn't be calculated at all. Rather, the appropriate matrix methods (e.g. QR decompositions ) built into R --

Re: [R] Using apply

2018-10-30 Thread Peter Langfelder
It should be said that for many basic statistics, there are faster functions than apply, for example here you want sum = colSums(x) As already said, for sum of squares you would do colSums(x^2). Many useful functions of this kind are implemented in package matrixStats. Once you install it,

Re: [R] Using apply

2018-10-30 Thread jim holtman
> s2 <- apply(x*x, 2, sum) > s2 [1] 55 330 Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Tue, Oct 30, 2018 at 10:28 PM Steven Yen wrote: > > I need help with "apply". Below, I have no problem getting

[R] Using apply

2018-10-30 Thread Steven Yen
I need help with "apply". Below, I have no problem getting the column sums. 1. How do I get the sum of squares? 2. In general, where do I look up these functions? Thanks. x<-matrix(1:10,nrow=5); x sum <- apply(x,2,sum); sum [[alternative HTML version deleted]]

Re: [R] Using apply function to merge list of data frames

2018-07-27 Thread Jeff Newmiller
Message- >> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of >Naresh >> Gurbuxani >> Sent: 25 July 2018 07:17 >> To: R-help@r-project.org >> Subject: [R] Using apply function to merge list of data frames >> >> I have a list whose

Re: [R] Using apply function to merge list of data frames

2018-07-27 Thread S Ellison
ed results. S Ellison > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Naresh > Gurbuxani > Sent: 25 July 2018 07:17 > To: R-help@r-project.org > Subject: [R] Using apply function to merge list of data frames > > I have a lis

Re: [R] Using apply function to merge list of data frames

2018-07-25 Thread Berend Hasselman
> On 25 Jul 2018, at 08:17, Naresh Gurbuxani > wrote: > > I have a list whose components are data frames. My goal is to construct a > data frame by merging all the list components. Is it possible to achieve > this using apply and without a for loop, as used below? > > Thanks, > Naresh >

Re: [R] using apply

2018-05-02 Thread David L Carlson
[3,]31 # [4,]32 David L Carlson Department of Anthropology Texas A University College Station, TX 77843-4352 -Original Message- From: R-help <r-help-boun...@r-project.org> On Behalf Of Ulrik Stervbo Sent: Wednesday, May 2, 2

Re: [R] using apply

2018-05-02 Thread Ulrik Stervbo
Hi Neha, Perhaps merge() from base or join from dplyr is what you are looking for. data. table could also be interesting. Hth Ulrik On Wed, 2 May 2018, 21:28 Neha Aggarwal, wrote: > Hi > > I have 3 dataframes, a,b,c with 0/1 values...i have to check a condition >

[R] using apply

2018-05-02 Thread Neha Aggarwal
Hi I have 3 dataframes, a,b,c with 0/1 values...i have to check a condition for dataframe a and b and then input the rows ids to datframe c . In the if condition, I AND the 2 rows of from a and b and then see if the result is equal to one of them. I have done this using a for loop, however, it

Re: [R] Using apply on a three dimensional matrix and passing multiple arguments to user defined function

2016-09-08 Thread Justin Peter
0%3cjvad...@usgs.gov%3e>>, Justin Peter <justin.pe...@usq.edu.au<mailto:justin%20peter%20%3cjustin.pe...@usq.edu.au%3e>> CC: r-help@r-project.org <r-help@r-project.org<mailto:%22r-h...@r-project.org%22%20%3cr-h...@r-project.org%3e>> Subject: Re: [R] Using apply on a th

Re: [R] Using apply on a three dimensional matrix and passing multiple arguments to user defined function

2016-09-07 Thread Luisfo via R-help
Hi, Jean's example with lapply works fine. However, if you still want to use apply, I think this works. One observation first. You were passing c(1,2) as second argument to apply, in your code. And that is what makes you have lots of NAs as a result, since your function is being applied twice,

Re: [R] Using apply on a three dimensional matrix and passing multiple arguments to user defined function

2016-09-07 Thread Jeff Newmiller
Should be working, so the devil is in the details you are not showing. Please provide a reproducible (self-contained) example [1] and post in plain text rather than HTML formatted email to avoid code corruption. [1] http://adv-r.had.co.nz/Reproducibility.html -- Sent from my phone. Please

Re: [R] Using apply on a three dimensional matrix and passing multiple arguments to user defined function

2016-09-07 Thread Adams, Jean
Justin, I don't think you can get the apply() function to return an array. You could use lapply() instead, and then use simplify2array() to convert the list of matrices to an array. Also, in your mask() function you don't need the which() and you should return the x. See my example with toy

[R] Using apply on a three dimensional matrix and passing multiple arguments to user defined function

2016-09-07 Thread Justin Peter
Dear R-user, I have a three-dimensional matrix of atmospheric data. The first two dimensions are spatial (lon and lat) and the third is time, such that dim(data) <- c(nlon,nlat,ntime) I wish to apply a land sea mask data which is a matrix of "0" and "1" if dim(nlon,nlat) dim(lsmask) <-

Re: [R] using apply to a data frame

2016-04-07 Thread David Winsemius
> On Apr 7, 2016, at 1:25 PM, John Sorkin wrote: > > > ‪‪I would like to apply a function, fract, to the columns of a > dataframe. I tried the following > apply(data5NonEventEpochs,2,fract) > but, no surprise it did not work as apply works on matrices not data >

Re: [R] using apply to a data frame

2016-04-07 Thread Peter Langfelder
Use lapply or sapply. A data frame is also a list with each component representing one column; lapply/sapply will apply the function to each column. Peter On Thu, Apr 7, 2016 at 1:25 PM, John Sorkin wrote: > > ‪‪I would like to apply a function, fract, to the

Re: [R] using apply to a data frame

2016-04-07 Thread Bert Gunter
Inline. -- Bert Bert Gunter On Thu, Apr 7, 2016 at 1:25 PM, John Sorkin wrote: > > ‪‪I would like to apply a function, fract, to the columns of a > dataframe. I tried the following > apply(data5NonEventEpochs,2,fract) > but, no surprise it did not work as apply

Re: [R] using apply to a data frame

2016-04-07 Thread Luisfo Chiroque via R-help
Dear John, Try using sapply(data5NonEventEpochs, fract) HTH Best, Luisfo Chiroque PhD Student IMDEA Networks Institute http://fourier.networks.imdea.org/people/~luis_nunez/ > El 7 abr 2016, a las 23:25, John Sorkin

[R] using apply to a data frame

2016-04-07 Thread John Sorkin
‪‪I would like to apply a function, fract, to the columns of a dataframe. I tried the following apply(data5NonEventEpochs,2,fract) but, no surprise it did not work as apply works on matrices not data frames. How can I apply a fuction to the columns of a data frame? (I can't covert

[R] Using apply() with functions I wrote

2014-07-24 Thread Pfeiffer, Steven
Hello! Does apply() not work with customized functions? Here is a simple example: AddSeven-function(n){n+7} AddSeven(3) [1] 10 M-matrix(nrow=2,ncol=2,data=c(1,2,3,4),byrow=TRUE) M [,1] [,2] [1,]12 [2,]34

Re: [R] Using apply() with functions I wrote

2014-07-24 Thread Bert Gunter
ummm R is case sensitive! fun != FUN (Have you gone through any R tutorials yet? If not, please do so before posting further). Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 Data is not information. Information is not knowledge. And knowledge is certainly not

Re: [R] Using apply() with functions I wrote

2014-07-24 Thread Pfeiffer, Steven
Ugghh... Sorry for bothering you all!! (Yes, I analyzed the data for my ecology M.S. project with R, but I wasted countless hours committing silly mistakes like this.) On Thu, Jul 24, 2014 at 4:11 PM, Bert Gunter gunter.ber...@gene.com wrote: ummm R is case sensitive! fun != FUN (Have

Re: [R] Using apply() with functions I wrote

2014-07-24 Thread Peter Alspach
. To: r-help@r-project.org Subject: [R] Using apply() with functions I wrote Hello! Does apply() not work with customized functions? Here is a simple example: AddSeven-function(n){n+7} AddSeven(3) [1] 10 M-matrix(nrow=2,ncol=2,data=c(1,2,3,4),byrow=TRUE) M [,1

Re: [R] Using apply with more than one matrix

2014-05-01 Thread David Winsemius
On Apr 30, 2014, at 6:03 PM, Waichler, Scott R wrote: Here is a working example with no random parts. Thanks for your patience and if I'm still off the mark with my presentation I'll drop the matter. v - c(NA, 1.5, NA, NA, NA, 1.1, 0.5, NA, NA, 1.3, 0.4, 0.9) a1 -

Re: [R] Using apply with more than one matrix

2014-05-01 Thread Waichler, Scott R
; improper indexing for a1 } } vec - sapply(seq(length(m2)), evaluate) Scott Waichler -Original Message- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: Wednesday, April 30, 2014 8:46 PM To: Waichler, Scott R Cc: Bert Gunter; r-help@r-project.org Subject: Re: [R] Using

Re: [R] Using apply with more than one matrix

2014-05-01 Thread David Winsemius
of relationships should be assumed? -- David. Scott Waichler -Original Message- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: Wednesday, April 30, 2014 8:46 PM To: Waichler, Scott R Cc: Bert Gunter; r-help@r-project.org Subject: Re: [R] Using apply with more than one

Re: [R] Using apply with more than one matrix

2014-05-01 Thread arun
Winsemius [mailto:dwinsem...@comcast.net] Sent: Wednesday, April 30, 2014 8:46 PM To: Waichler, Scott R Cc: Bert Gunter; r-help@r-project.org Subject: Re: [R] Using apply with more than one matrix On Apr 30, 2014, at 6:03 PM, Waichler, Scott R wrote: Here is a working example with no random

Re: [R] Using apply with more than one matrix

2014-05-01 Thread arun
)), evaluate) Scott Waichler -Original Message- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: Wednesday, April 30, 2014 8:46 PM To: Waichler, Scott R Cc: Bert Gunter; r-help@r-project.org Subject: Re: [R] Using apply with more than one matrix On Apr 30, 2014, at 6:03

Re: [R] Using apply with more than one matrix

2014-05-01 Thread Waichler, Scott R
Sapply or mapply may work, I haven't used these much and will try to learn better how to use them. Your use of sapply looks good; but I'm trying to understand if and how I can bring in the operation on a1. This doesn't work: evaluate - function(idx) { ind.not.na -

Re: [R] Using apply with more than one matrix

2014-05-01 Thread Waichler, Scott R
: Bert Gunter; r-help@r-project.org Subject: Re: [R] Using apply with more than one matrix On Apr 30, 2014, at 6:03 PM, Waichler, Scott R wrote: Here is a working example with no random parts.  Thanks for your patience and if I'm still off the mark with my presentation I'll drop

Re: [R] Using apply with more than one matrix

2014-05-01 Thread William Dunlap
will be more efficient. Thanks also to David and the others who responded to my question. It all helped. --Scott Waichler -Original Message- From: arun [mailto:smartpink...@yahoo.com] Sent: Thursday, May 01, 2014 6:24 AM To: R. Help Cc: Waichler, Scott R Subject: Re: [R] Using apply

[R] Using apply with more than one matrix

2014-04-30 Thread Waichler, Scott R
Hi, I want to apply a custom function to all the elements of one matrix. The function uses the value in the same i,j in a second matrix. How can I use apply() or similar function to avoid nested loops in i and j? Thanks, Scott Waichler Pacific Northwest National Laboratory Richland, WA, USA

Re: [R] Using apply with more than one matrix

2014-04-30 Thread Ranjan Maitra
Hi Scott, You could set up a three-dimensional array and then use apply on the desired dimension (MARGIN in apply language). HTH! Many thanks, Ranjan On Wed, 30 Apr 2014 17:54:48 + Waichler, Scott R scott.waich...@pnnl.gov wrote: Hi, I want to apply a custom function to all the

Re: [R] Using apply with more than one matrix

2014-04-30 Thread Bert Gunter
Scott: Your problem specification is rather vague: What do you mean by use? In general, matrices are merely vectors with a dim attribute, so if you can do what you want with them as vectors, then that will work for them as matrices. For example: m1- matrix(1:6, nr=3) m2 - matrix(11:16, nr=2)

Re: [R] Using apply with more than one matrix

2014-04-30 Thread Waichler, Scott R
] - a1[i,j,ind.not.na[1]] + m2[i,j] } } } Scott Waichler -Original Message- From: Bert Gunter [mailto:gunter.ber...@gene.com] Sent: Wednesday, April 30, 2014 12:18 PM To: Waichler, Scott R Cc: r-help@r-project.org Subject: Re: [R] Using apply with more than one matrix Scott

Re: [R] Using apply with more than one matrix

2014-04-30 Thread David Winsemius
[mailto:gunter.ber...@gene.com] Sent: Wednesday, April 30, 2014 12:18 PM To: Waichler, Scott R Cc: r-help@r-project.org Subject: Re: [R] Using apply with more than one matrix Scott: Your problem specification is rather vague: What do you mean by use? In general, matrices are merely vectors

Re: [R] Using apply with more than one matrix

2014-04-30 Thread Waichler, Scott R
Here is a working example with no random parts. Thanks for your patience and if I'm still off the mark with my presentation I'll drop the matter. v - c(NA, 1.5, NA, NA, NA, 1.1, 0.5, NA, NA, 1.3, 0.4, 0.9) a1 - array(v, dim=c(2,2,3)) m1 - matrix(c(NA, 1.5, 2.1, NA), ncol=2,

Re: [R] Using apply with more than one matrix

2014-04-30 Thread Richard M. Heiberger
Try ?mapply Description: 'mapply' is a multivariate version of 'sapply'. 'mapply' applies 'FUN' to the first elements of each ... argument, the second elements, the third elements, and so on. Arguments are recycled if necessary. Rich On Wed, Apr 30, 2014 at 9:03 PM,

[R] Using apply function

2014-01-27 Thread Yen Lee
Hi all R-users, I'm trying to using apply function to input a range of values into a function I wrote. I wrote a function with 4 information needed. I would like to make 2 of them fixed and the other 2 random (but with specified values). I would like to replicate the function 1 times. I

Re: [R] Using apply function

2014-01-27 Thread jim holtman
Is this what you want: random - expand.grid(R1 = 1:5, R2 = -(1:5)) result - cbind(F1 = 10, F2 = 100, random) result F1 F2 R1 R2 1 10 100 1 -1 2 10 100 2 -1 3 10 100 3 -1 4 10 100 4 -1 5 10 100 5 -1 6 10 100 1 -2 7 10 100 2 -2 8 10 100 3 -2 9 10 100 4 -2 10 10 100 5 -2

[R] using 'apply' to apply princomp to an array of datasets

2012-12-12 Thread David Romano
Hi everyone, Suppose I have a 3D array of datasets, where say dimension 1 corresponds to cases, dimension 2 to datasets, and dimension 3 to observations within a dataset. As an example, suppose I do the following: x - sample(1:20, 48, replace=TRUE) datasets - array(x, dim=c(4,3,2)) Here, for

Re: [R] using 'apply' to apply princomp to an array of datasets

2012-12-12 Thread David Romano
Sorry, I just realized I didn't send the message below in plain text. -David Romano On Wed, Dec 12, 2012 at 9:14 AM, David Romano drom...@stanford.edu wrote: Hi everyone, Suppose I have a 3D array of datasets, where say dimension 1 corresponds to cases, dimension 2 to datasets, and dimension

Re: [R] using 'apply' to apply princomp to an array of datasets

2012-12-12 Thread Rui Barradas
Hello, As for the first question try scoreset - lapply(pcl, function(x) x$scores[, 1]) do.call(cbind, scoreset) As for the second question, you want to know which columns in 'datasets' have NA's? colidx - apply(datasets, 2, function(x) any(is.na(x))) datasets[, colidx] # These have NA's

Re: [R] using 'apply' to apply princomp to an array of datasets

2012-12-12 Thread David Romano
Thank you, Rui! This is incredibly helpful -- anonymous functions are new to me, and I appreciate being shown how useful they are. Best regards, David On Wed, Dec 12, 2012 at 10:12 AM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, As for the first question try scoreset - lapply(pcl,

Re: [R] Using apply instead of for loop / multithreading

2012-11-13 Thread Flavio Barros
That worked better because of vectorization, but isn't multithreaded. To have this resource look at plyr package. On Mon, Nov 12, 2012 at 9:08 PM, Charles D. charlybeg...@live.fr wrote: it works really faster ! thank you -- View this message in context:

[R] Using apply instead of for loop / multithreading

2012-11-12 Thread Charles D.
Hello , I'm new to R and don't really understand how to use the function apply instead of a for loop, particularly for a function with multiple entries. I have a big data file and would like to apply a function in multi thread to accelerate the processus. I have a data frame containing values

Re: [R] Using apply instead of for loop / multithreading

2012-11-12 Thread jim holtman
Since you did not provide sample data (use dput instead of posting links to 'jpg' files), you can probably do it without loops: k=resp[,5] CO2umol - resp[,6]*((Press*infoch[k,11]*1e-6)/(R*(resp[,7]+273.15))) CO2v - CO2umol/(infoch[k,10]) CO2s - CO2umol/(infoch[k,9] On Mon, Nov 12, 2012 at 12:39

Re: [R] Using apply instead of for loop / multithreading

2012-11-12 Thread Charles D.
it works really faster ! thank you -- View this message in context: http://r.789695.n4.nabble.com/Using-apply-instead-of-for-loop-multithreading-tp4649326p4649346.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org

Re: [R] using apply with sparse matrix from package Matrix

2012-09-04 Thread Martin Maechler
Jennifer Lyon jennifer.s.l...@gmail.com on Fri, 31 Aug 2012 17:22:57 -0600 writes: Hi: I was trying to use apply on a sparse matrix from package Matrix, and I get the error: Error in asMethod(object) : Cholmod error 'problem too large' at file

Re: [R] using apply with sparse matrix from package Matrix

2012-09-04 Thread Jennifer Lyon
On Tue, Sep 4, 2012 at 10:58 AM, Martin Maechler maech...@stat.math.ethz.ch wrote: Jennifer Lyon jennifer.s.l...@gmail.com on Fri, 31 Aug 2012 17:22:57 -0600 writes: Hi: I was trying to use apply on a sparse matrix from package Matrix, and I get the error: Error in

[R] using apply with sparse matrix from package Matrix

2012-08-31 Thread Jennifer Lyon
Hi: I was trying to use apply on a sparse matrix from package Matrix, and I get the error: Error in asMethod(object) : Cholmod error 'problem too large' at file ../Core/cholmod_dense.c, line 106 Is there a way to apply a function to all the rows without bumping into this problem? Here is a

[R] Using apply() with a function involving ode()

2012-04-25 Thread Adam Zeilinger
Hello, I am trying to get the output from the numerical simulation of a system of ordinary differential equations for a range of values for three parameters. I am using the ode() function (deSolve package) to run the numerical simulation and apply() to run the simulation function for each

Re: [R] Using apply() with a function involving ode()

2012-04-25 Thread Berend Hasselman
See inline comments. On 25-04-2012, at 08:40, Adam Zeilinger wrote: Hello, I am trying to get the output from the numerical simulation of a system of ordinary differential equations for a range of values for three parameters. I am using the ode() function (deSolve package) to run the

Re: [R] Using apply() with a function involving ode()

2012-04-25 Thread Adam Zeilinger
Dear Berend, Yes, the wnv.sim function should have included wnv.model instead of wnv.incubation. Sorry for the typo. Although I had inspected the expand.grid() object, I had not connected the error with a naming problem. Thanks so much for the help! The function works perfectly.

Re: [R] using apply function and storing output

2010-10-15 Thread Dennis Murphy
help me with the error? Dave -- Date: Fri, 15 Oct 2010 00:45:08 -0700 Subject: Re: [R] using apply function and storing output From: djmu...@gmail.com To: dasol...@hotmail.com CC: r-help@r-project.org Hi: Look into the rollmean() function in package zoo

Re: [R] using apply function and storing output

2010-10-15 Thread David A.
, the rollapply() function now works fine. Thanks again David Date: Fri, 15 Oct 2010 03:00:27 -0700 Subject: Re: [R] using apply function and storing output From: djmu...@gmail.com To: dasol...@hotmail.com CC: r-help@r-project.org Hi: You need to give a function for rollapply() to apply

Re: [R] using apply function and storing output

2010-10-15 Thread Gabor Grothendieck
On Fri, Oct 15, 2010 at 6:46 AM, David A. dasol...@hotmail.com wrote: Thanks Dennis, I don't think it was a problem of not feeding in a function for rollapply(), because I was using mean() and my co.var() function in the FUN argument. The key part seems to be the transformation that zoo()

[R] using apply function and storing output

2010-10-15 Thread David A.
Hi list, I have a 1710x244 matrix of numerical values and I would like to calculate the mean of every group of three consecutive values per column to obtain a new matrix of 570x244. I could get it done using a for loop but how can I do that using apply functions? In addition to this, do I

Re: [R] using apply function and storing output

2010-10-15 Thread Dennis Murphy
Hi: Look into the rollmean() function in package zoo. HTH, Dennis On Fri, Oct 15, 2010 at 12:34 AM, David A. dasol...@hotmail.com wrote: Hi list, I have a 1710x244 matrix of numerical values and I would like to calculate the mean of every group of three consecutive values per column to

Re: [R] using apply function and storing output

2010-10-15 Thread Dimitris Rizopoulos
one efficient way to do this, avoiding loops, is using the rowsum() function, e.g., mat - matrix(rnorm(1710*244), 1710, 244) id - rep(seq_len(570), each = 3) means - rowsum(mat, id, FALSE) / 3 Regarding the second part of your question, indeed a gold rule of efficient R programming when it

Re: [R] using apply function and storing output

2010-10-15 Thread David A.
) : no applicable method for 'rollapply' applied to an object of class c('matrix', 'integer', 'numeric') Can you help me with the error? Dave Date: Fri, 15 Oct 2010 00:45:08 -0700 Subject: Re: [R] using apply function and storing output From: djmu...@gmail.com To: dasol...@hotmail.com CC: r-help@r

[R] Using apply for logical conditions

2010-08-02 Thread Alastair
Hi, I've got some boolean data in a data.frame in the form: XYZA B C [1] T TFT F F [2] F TTF F F . . . What I want to do is create a new column which is the logical disjunction of several of the columns. Just like: new.column - data$X |

Re: [R] Using apply for logical conditions

2010-08-02 Thread Allan Engelhardt
`|` is a binary operator which is why the apply will not work. See help(Reduce) For example, set.seed(1) data - data.frame(A = runif(10) 0.5, B = runif(10) 0.5, C = runif(10) 0.5) Reduce(`|`, data) # [1] TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE Hope this helps.

Re: [R] Using apply for logical conditions

2010-08-02 Thread Erik Iverson
Alastair wrote: Hi, I've got some boolean data in a data.frame in the form: XYZA B C [1] T TFT F F [2] F TTF F F . . . What I want to do is create a new column which is the logical disjunction of several of the columns. Just like:

Re: [R] Using apply for logical conditions

2010-08-02 Thread Joshua Wiley
In addition to Reduce(), you can take a look at ?any for '|' and ?all for ''. Josh On Mon, Aug 2, 2010 at 1:43 PM, Allan Engelhardt all...@cybaea.com wrote: `|` is a binary operator which is why the apply will not work.  See help(Reduce) For example, set.seed(1) data - data.frame(A =

Re: [R] Using apply for logical conditions

2010-08-02 Thread Michael Lachmann
Reduce() is much nicer, but I usually use rowSums(A) 0 for 'or', and rowSums(A) == ncols for 'and'. Which works slightly faster. I noticed, though, that Reduce() doesn't work on matrices. Is there an alternative for matrices, or do you have to convert the matrix first to a data.frame, and

Re: [R] Using apply for logical conditions

2010-08-02 Thread Bert Gunter
Yes, you must do the conversion. The reason is that Reduce requires its argument x, to be a vector; and a matrix is seen a vector obtained by columnwise concatenation. e.g. Reduce(+,matrix(1:6,nr=3)) [1] 21 Reduce(+,1:6) [1] 21 The data frame is seen as a list with elements the columns of the

Re: [R] Using apply for logical conditions

2010-08-02 Thread Joshua Wiley
On Mon, Aug 2, 2010 at 2:08 PM, Michael Lachmann lachm...@eva.mpg.de wrote: Reduce() is much nicer, but I usually use rowSums(A) 0 for 'or', and rowSums(A) == ncols for 'and'. Which works slightly faster. For the sake of my own curiosity, I compared several of these options, but in case

Re: [R] Using apply for logical conditions

2010-08-02 Thread Michael Lachmann
Reduce() is really amazingly fast! Even with a much larger number of columns, it is still in the same ballpark (and much more readable): boolean - c(TRUE, rep(FALSE,10^3)) a-matrix(sample(boolean, 10^7, replace = TRUE),10^4,10^3) b-data.frame(a) system.time({opt4 - rowSums(a, na.rm = TRUE)

Re: [R] Using apply for logical conditions

2010-08-02 Thread Bert Gunter
Just for fun, here are another couple of versions that work for data frames. For Reduce with | do.call(pmax,c(mydata,na.rm=TRUE)) 0 and for do.call(pmin,c(mydata,na.rm=TRUE)) 0  Cheers, Bert Gunter Genentech Nonclinical Biostatistics On Mon, Aug 2, 2010 at 2:28 PM, Joshua Wiley

Re: [R] Using apply for logical conditions

2010-08-02 Thread Alastair
Wow, Thanks for all the excellent (and fast) responses. That's really helped. Sorry I didn't supply a cut and paste-able example (noted for future reference) but your examples caught the essence of my problem. I ended up opting for the apply any solution. But I'll bear the Reduce function in

[R] Using apply function on duplicates in a data.frame

2010-01-31 Thread Sunny Srivastava
Dear R-Helpers, I have a data.frame (df) and the head of data.frame looks like ProbeUID ControlType ProbeName GeneName SystematicName 1665 1577 0 pSysX_50_22_1 pSysX_50 pSysX_50 5422 5147 0 pSysX_49_8_1 pSysX_49 pSysX_49 4042 3843 0

Re: [R] Using apply function on duplicates in a data.frame

2010-01-31 Thread David Winsemius
On Jan 31, 2010, at 6:05 PM, Sunny Srivastava wrote: Dear R-Helpers, I have a data.frame (df) and the head of data.frame looks like ProbeUID ControlType ProbeName GeneName SystematicName 1665 1577 0 pSysX_50_22_1 pSysX_50 pSysX_50 5422 5147 0

Re: [R] Using apply function on duplicates in a data.frame

2010-01-31 Thread hadley wickham
On Sun, Jan 31, 2010 at 5:05 PM, Sunny Srivastava research.b...@gmail.com wrote: Dear R-Helpers, I have a data.frame (df) and the head of data.frame looks like     ProbeUID ControlType     ProbeName GeneName SystematicName 1665     1577           0 pSysX_50_22_1 pSysX_50       pSysX_50 5422  

Re: [R] Using apply function on duplicates in a data.frame

2010-01-31 Thread Sunny Srivastava
Thanks a lot. All the three approaches work for me! On Sun, Jan 31, 2010 at 10:43 PM, hadley wickham h.wick...@gmail.comwrote: On Sun, Jan 31, 2010 at 5:05 PM, Sunny Srivastava research.b...@gmail.com wrote: Dear R-Helpers, I have a data.frame (df) and the head of data.frame looks like

[R] Using apply() and scale() in combination

2009-05-18 Thread Armin Raznahan
Hello I am an R newbie, and am coming against a couple of problems when I try and apply the scale function across all the rows of a matrix. - #I have a matrix dt_l. str(dt_l) num [1:40962, 1:885] 3.04 4.1 3.4 3.58 3.77 ... #I want to convert the values in each row of

Re: [R] Using apply() and scale() in combination

2009-05-18 Thread Peter Alspach
(). HTH . Peter Alspach -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Armin Raznahan Sent: Tuesday, 19 May 2009 9:44 a.m. To: r-help@r-project.org Subject: [R] Using apply() and scale() in combination Hello I am

[R] Using apply to get group means

2009-03-31 Thread Alan Cohen
Hi all, I'm trying to improve my R skills and make my programming more efficient and succinct. I can solve the following question, but wonder if there's a better way to do it: I'm trying to calculate mean by several variables and then put this back into the original data set as a new

Re: [R] Using apply to get group means

2009-03-31 Thread baptiste auguie
Not exactly the output you asked for, but perhaps you can consider, library(doBy) summaryBy(x3~x2+x1,data=x,FUN=mean) x2 x1 x3.mean 1 1 A 1.5 2 1 B 2.0 3 1 C 3.5 4 2 A 4.0 5 2 B 5.5 6 2 C 6.0 the plyr package also provides similar functionality, as do

Re: [R] Using apply to get group means

2009-03-31 Thread hadley wickham
On Tue, Mar 31, 2009 at 11:31 AM, baptiste auguie ba...@exeter.ac.uk wrote: Not exactly the output you asked for, but perhaps you can consider, library(doBy) summaryBy(x3~x2+x1,data=x,FUN=mean)  x2 x1 x3.mean 1  1  A     1.5 2  1  B     2.0 3  1  C     3.5 4  2  A     4.0 5  2  B    

Re: [R] Using apply to get group means

2009-03-31 Thread Domenico Vistocco
A different solution (using aggregate for the table of means and merge for adding it to the dataframe): x1-rep(c(A,B,C),3) x2-c(rep(1,3),rep(2,3),1,2,1) x3-c(1,2,3,4,5,6,2,6,4) x-data.frame(x1,x2,x3) #here using data.frame the x1 variable is directly converted to factor x3means -

Re: [R] Using apply to get group means

2009-03-31 Thread Domenico Vistocco
Sorry, there was a mistake in the previous mail: Domenico Vistocco wrote: A different solution (using aggregate for the table of means and merge for adding it to the dataframe): x1-rep(c(A,B,C),3) x2-c(rep(1,3),rep(2,3),1,2,1) x3-c(1,2,3,4,5,6,2,6,4) x-data.frame(x1,x2,x3) #here using

Re: [R] Using apply to get group means

2009-03-31 Thread David Winsemius
That is precisely the reason for the existence of the ave function. Using Wickham's example: x1 - rep(c(A, B, C), 3) x2 - c(rep(1, 3), rep(2, 3), 1, 2, 1) x3 - c(1, 2, 3, 4, 5, 6, 2, 6, 4) df - data.frame(x1, x2, x3) df$grpx3 - ave(df$x3, list(x1,x2)) df x1 x2 x3 grpx3 1 A 1 1

[R] Using apply to generate matrix from rows?

2009-01-19 Thread Stephan Lindner
Dear all, I have a simple question which I unfortunately do not seem to be able to solve myself. I have a (NxK) matrix and want to generate a new matrix by multiplying each row with itself such that the new matrix has dimension ((N*K)xK) (or better, generate an array with dimension (K,K,N)). I

Re: [R] Using apply to generate matrix from rows?

2009-01-19 Thread Henrique Dallazuanna
Try this: matrix(apply(u, 1, tcrossprod), nr = nrow(u)*ncol(u), byrow = T) On Mon, Jan 19, 2009 at 3:39 PM, Stephan Lindner lindn...@umich.edu wrote: Dear all, I have a simple question which I unfortunately do not seem to be able to solve myself. I have a (NxK) matrix and want to generate

Re: [R] Using apply to generate matrix from rows?

2009-01-19 Thread Jorge Ivan Velez
Dear Stephan, Try this: do.call(rbind,lapply(1:2,function(x) matrix(u[x,]%*%t(u[x,]),ncol=ncol(u HTH, Jorge On Mon, Jan 19, 2009 at 12:39 PM, Stephan Lindner lindn...@umich.eduwrote: Dear all, I have a simple question which I unfortunately do not seem to be able to solve myself. I

[R] Using apply for two datasets

2009-01-08 Thread Christos Argyropoulos
It depends on how the data are arranged ## x-matrix(c(1,2,3,2,8,2,4,5,6),nrow=3) y-matrix(c(10,2,13,0,8,4,4.2,5.2,6.2),nrow=3) q-mapply(t.test,as.data.frame(x),as.data.frame(y)) q ## The ith column of q contain the results of applying t.test

[R] Using apply for two datasets

2009-01-06 Thread Gang Chen
I can run one-sample t-test on an array, for example a matrix myData1, with the following apply(myData1, 2, t.test) Is there a similar fashion using apply() or something else to run 2-sample t-test with datasets from two groups, myData1 and myData2, without looping? TIA, Gang

Re: [R] Using apply for two datasets

2009-01-06 Thread Henrique Dallazuanna
I think that you can use mapply for this. On Tue, Jan 6, 2009 at 3:24 PM, Gang Chen gangch...@gmail.com wrote: I can run one-sample t-test on an array, for example a matrix myData1, with the following apply(myData1, 2, t.test) Is there a similar fashion using apply() or something else to

Re: [R] Using apply for two datasets

2009-01-06 Thread Jorge Ivan Velez
Hi Gang, Perhaps this post might be useful in this case. Please take a special lookat Gábor Csárdi's reply. http://www.nabble.com/apply,-t-test-and-p-values-to20012292.html#a20012292 HTH, Jorge On Tue, Jan 6, 2009 at 1:10 PM, Gang Chen gangch...@gmail.com wrote: Thanks a lot for the quick

Re: [R] Using apply for two datasets

2009-01-06 Thread Satoshi Takahama
To: Henrique Dallazuanna www...@gmail.com Cc: r-h...@stat.math.ethz.ch Sent: Tuesday, January 6, 2009 10:10:44 AM Subject: Re: [R] Using apply for two datasets Thanks a lot for the quick help! mapply() seems promising. However, mapply(t.test, myData1, myData2) would not work, so how can I specify

Re: [R] Using apply for two datasets

2009-01-06 Thread Gang Chen
Subject: Re: [R] Using apply for two datasets Thanks a lot for the quick help! mapply() seems promising. However, mapply(t.test, myData1, myData2) would not work, so how can I specify the margin in mapply() which function t.test() will be applied over? For example, I specify the 2nd dimension

[R] using apply to loop

2007-12-20 Thread Louis Martin
Hi, I am running the following loop, but it takes hours to run as n is big. Is there any way apply can be used? Thanks. ### Start nclass - dim(data)[[2]] - 1 z - matrix(0, ncol = nclass, nrow = nclass) n - dim(data)[[1]] x - c(1:nclass) # loop starts for(loop in 1:n) { r

Re: [R] using apply to loop [SEC=UNCLASSIFIED]

2007-12-20 Thread Crombie, Joe
PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Louis Martin Sent: Friday, 21 December 2007 11:37 AM To: R-help@r-project.org Subject: [R] using apply to loop Hi, I am running the following loop, but it takes hours to run as n is big. Is there any way apply can be used? Thanks. ### Start nclass

Re: [R] using apply to loop

2007-12-20 Thread Tyler Smith
On 2007-12-21, Louis Martin [EMAIL PROTECTED] wrote: Hi, I am running the following loop, but it takes hours to run as n is big. Is there any way apply can be used? Thanks. ### Start nclass - dim(data)[[2]] - 1 z - matrix(0, ncol = nclass, nrow = nclass) n - dim(data)[[1]]