[R] tapply with cbinded x

2009-06-16 Thread Stefan Uhmann
Dear List, why does this not work? df - data.frame(var1 = c(3,2,1), var2 = c(6,5,4), var3 = c(9,8,7), fac = c('A', 'A', 'B')) tapply(cbind(df$var1, df$var2, df$var3), df$fac, mean) Thank you, Stefan __ R-help@r-project.org mailing list

Re: [R] tapply with cbinded x

2009-06-16 Thread Wacek Kusnierczyk
Stefan Uhmann wrote: Dear List, why does this not work? df - data.frame(var1 = c(3,2,1), var2 = c(6,5,4), var3 = c(9,8,7), fac = c('A', 'A', 'B')) tapply(cbind(df$var1, df$var2, df$var3), df$fac, mean) because length(cbind(df$var1, df$var2, df$var3)) # 9 length(df$fac)

Re: [R] tapply with cbinded x

2009-06-16 Thread Stavros Macrakis
On Tue, Jun 16, 2009 at 5:16 AM, Stefan Uhmann stefan.uhm...@googlemail.com wrote: why does this not work? df - data.frame(var1 = c(3,2,1), var2 = c(6,5,4), var3 = c(9,8,7), fac = c('A', 'A', 'B')) tapply(cbind(df$var1, df$var2, df$var3), df$fac, mean) Because tapply is defined for

Re: [R] tapply with cbinded x

2009-06-16 Thread Petr PIKAL
Hi r-help-boun...@r-project.org napsal dne 16.06.2009 12:45:04: Stefan Uhmann wrote: Dear List, why does this not work? df - data.frame(var1 = c(3,2,1), var2 = c(6,5,4), var3 = c(9,8,7), fac = c('A', 'A', 'B')) tapply(cbind(df$var1, df$var2, df$var3), df$fac, mean) because

Re: [R] tapply with cbinded x

2009-06-16 Thread Wacek Kusnierczyk
Stavros Macrakis wrote: On Tue, Jun 16, 2009 at 5:16 AM, Stefan Uhmann stefan.uhm...@googlemail.com wrote: why does this not work? df - data.frame(var1 = c(3,2,1), var2 = c(6,5,4), var3 = c(9,8,7), fac = c('A', 'A', 'B')) tapply(cbind(df$var1, df$var2, df$var3),

Re: [R] tapply with cbinded x

2009-06-16 Thread Wacek Kusnierczyk
Petr PIKAL wrote: Hi r-help-boun...@r-project.org napsal dne 16.06.2009 12:45:04: Stefan Uhmann wrote: Dear List, why does this not work? df - data.frame(var1 = c(3,2,1), var2 = c(6,5,4), var3 = c(9,8,7), fac = c('A', 'A', 'B')) tapply(cbind(df$var1, df$var2, df$var3),

[R] tapply changing order of factor levels?

2009-05-06 Thread Chirantan Kundu
Hi, Does tapply change the order when applied on a factor? Below is the code I tried. mylevels-c(IN0020020155,IN0019800021,IN0020020064) mydata-c(IN0020020155,IN0019800021,IN0020020064,IN0020020155,IN0019800021,IN0019800021,IN0020020064,IN0020020064,IN0019800021)

Re: [R] tapply changing order of factor levels?

2009-05-06 Thread jim holtman
The result of 'tapply' is just a named vector and the names are in alphabetical order. If you want them printed in a different order, then you have to specify it. Since you have the order in 'mylevels', this will work: str(mysummary) int [1:3(1d)] 4 3 2 - attr(*, dimnames)=List of 1 ..$ :

Re: [R] tapply changing order of factor levels?

2009-05-06 Thread Chirantan Kundu
Thanks, Jim. It works! On Wed, May 6, 2009 at 7:26 PM, jim holtman jholt...@gmail.com wrote: The result of 'tapply' is just a named vector and the names are in alphabetical order. If you want them printed in a different order, then you have to specify it. Since you have the order in

Re: [R] tapply changing order of factor levels?

2009-05-06 Thread Alain Guillet
Hi, I don't believe the problem is related to tapply. I would say it is because of the factor. In fact, the order of a factor is given by the alphanumerical order of his levels. You can see it with levels(myfactor). I you want to change the order, redefine the levels of myfactor with the

Re: [R] tapply changing order of factor levels?

2009-05-06 Thread Chirantan Kundu
Hi Alain, I tried levels(myfactor) as you suggested. levels(myfactor) [1] IN0020020155 IN0019800021 IN0020020064 The order is preserved, no alphanumerical sorting done here. Regards. On Wed, May 6, 2009 at 7:35 PM, Alain Guillet alain.guil...@uclouvain.bewrote: Hi, I don't believe the

Re: [R] tapply changing order of factor levels?

2009-05-06 Thread Chirantan Kundu
Hi Ellison, Thanks for pointing it out. Bad miss on my part. Regards. On Wed, May 6, 2009 at 7:55 PM, S Ellison s.elli...@lgc.co.uk wrote: The problem is in the code. When you say mysummary-tapply(myfactor,mydata,length) mysummary you have used mydata as a factor and myfactor as the

Re: [R] tapply changing order of factor levels?

2009-05-06 Thread S Ellison
The problem is in the code. When you say mysummary-tapply(myfactor,mydata,length) mysummary you have used mydata as a factor and myfactor as the data. tapply has (correctly) used the ordered labels in the grouping factor (mydata) to label its output. If you did what you probably intended:

Re: [R] tapply changing order of factor levels?

2009-05-06 Thread Bert Gunter
Genentech Nonclinical Statistics -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of jim holtman Sent: Wednesday, May 06, 2009 6:57 AM To: Chirantan Kundu Cc: r-help@r-project.org Subject: Re: [R] tapply changing order of factor levels

Re: [R] tapply changing order of factor levels?

2009-05-06 Thread Alain Guillet
Hi, I meant that your problem occured because the levels of mylevels are not ordered whereas tapply uses the ordered levels for printing. If you order them (look under), you can see the results of the tapply has the same order as the levels of myfactor

[R] tapply output as a dataframe

2009-04-13 Thread Dan Dube
i use tapply and by often, but i always end up banging my head against the wall with the output. is there a simpler way to convert the output of the following tapply to a dataframe or matrix than what i have here: # setup data for tapply dt = data.frame(bucket=rep(1:4,25),val=rnorm(100)) fn =

Re: [R] tapply output as a dataframe

2009-04-13 Thread Jorge Ivan Velez
Dear Dan, Try this: do.call(rbind,a) HTH, Jorge On Mon, Apr 13, 2009 at 12:41 PM, Dan Dube dd...@advisen.com wrote: i use tapply and by often, but i always end up banging my head against the wall with the output. is there a simpler way to convert the output of the following tapply to a

Re: [R] tapply output as a dataframe

2009-04-13 Thread jim holtman
do.call(rbind,a) [,1] [,2] [,3] [,4] 1 -0.7871502 -0.4437714 0.4011135 -0.2626129 2 -0.9546515 0.2210001 0.816 0.1245766 3 -0.5389725 -0.2750984 0.6655951 -0.1873485 4 -0.8176898 -0.1844181 0.4737187 -0.2688996 On Mon, Apr 13, 2009 at 12:41 PM, Dan Dube

Re: [R] tapply output as a dataframe

2009-04-13 Thread Dan Dube
this is what i needed! thank you. -Original Message- From: Jorge Ivan Velez [mailto:jorgeivanve...@gmail.com] Sent: Monday, April 13, 2009 12:50 PM To: Dan Dube Cc: r-help@r-project.org Subject: Re: [R] tapply output as a dataframe Dear Dan, Try this: do.call(rbind

[R] tapply bug? - levels of a factor in a data frame after tapply are intermixed

2009-02-13 Thread Dimitri Liakhovitski
Hello! I have encountered a really weird problem. Maybe you've encountered it before? I have a large data frame importances. It has one factor ($A) with 3 levels: 3, 9, and 15. $B is a regular numeric variable. Below I am picking a really small sub-frame (just 3 rows) based on indices. indices

Re: [R] tapply bug? - levels of a factor in a data frame after tapply are intermixed

2009-02-13 Thread Marc Schwartz
on 02/13/2009 11:09 AM Dimitri Liakhovitski wrote: Hello! I have encountered a really weird problem. Maybe you've encountered it before? I have a large data frame importances. It has one factor ($A) with 3 levels: 3, 9, and 15. $B is a regular numeric variable. Below I am picking a really

Re: [R] tapply bug? - levels of a factor in a data frame after tapply are intermixed

2009-02-13 Thread Dimitri Liakhovitski
On Fri, Feb 13, 2009 at 12:24 PM, Marc Schwartz marc_schwa...@comcast.net wrote: on 02/13/2009 11:09 AM Dimitri Liakhovitski wrote: Hello! I have encountered a really weird problem. Maybe you've encountered it before? I have a large data frame importances. It has one factor ($A) with 3

Re: [R] tapply bug? - levels of a factor in a data frame after tapply are intermixed

2009-02-13 Thread Greg Snow
: R-Help List Subject: Re: [R] tapply bug? - levels of a factor in a data frame after tapply are intermixed Sorry - one clarification: When I run: test$xx - the what I am currently seeing is: [1] 9 3 15 Levels: 3 9 15 But what I am expecting to be seeing is: [1] 9 3 15 Levels: 9

Re: [R] tapply bug? - levels of a factor in a data frame after tapply are intermixed

2009-02-13 Thread Marc Schwartz
on 02/13/2009 11:38 AM Dimitri Liakhovitski wrote: On Fri, Feb 13, 2009 at 12:24 PM, Marc Schwartz marc_schwa...@comcast.net wrote: on 02/13/2009 11:09 AM Dimitri Liakhovitski wrote: Hello! I have encountered a really weird problem. Maybe you've encountered it before? I have a large data

Re: [R] tapply bug? - levels of a factor in a data frame after tapply are intermixed

2009-02-13 Thread Dimitri Liakhovitski
- project.org] On Behalf Of Dimitri Liakhovitski Sent: Friday, February 13, 2009 10:54 AM To: marc_schwa...@comcast.net Cc: R-Help List Subject: Re: [R] tapply bug? - levels of a factor in a data frame after tapply are intermixed Sorry - one clarification: When I run: test$xx - the what I am currently

Re: [R] tapply within a data.frame: a simpler alternative?

2008-12-11 Thread David Freedman
You might take a look at the transformBy function in the doBy package For example, new.df=transformBy(~group,data=my.df, new=y/max(y)) David Freedman baptiste auguie-2 wrote: Dear list, I have a data.frame with x, y values and a 3-level factor group, say. I want to create a new

[R] tapply within a data.frame: a simpler alternative?

2008-12-10 Thread baptiste auguie
Dear list, I have a data.frame with x, y values and a 3-level factor group, say. I want to create a new column in this data.frame with the values of y scaled to 1 by group. Perhaps the example below describes it best: x - seq(0, 10, len=100) my.df - data.frame(x = rep(x, 3), y=c(3*sin(x),

Re: [R] tapply within a data.frame: a simpler alternative?

2008-12-10 Thread Chuck Cleland
On 12/10/2008 12:02 PM, baptiste auguie wrote: Dear list, I have a data.frame with x, y values and a 3-level factor group, say. I want to create a new column in this data.frame with the values of y scaled to 1 by group. Perhaps the example below describes it best: x - seq(0, 10, len=100)

Re: [R] tapply within a data.frame: a simpler alternative?

2008-12-10 Thread hadley wickham
On Wed, Dec 10, 2008 at 11:02 AM, baptiste auguie [EMAIL PROTECTED] wrote: Dear list, I have a data.frame with x, y values and a 3-level factor group, say. I want to create a new column in this data.frame with the values of y scaled to 1 by group. Perhaps the example below describes it best:

Re: [R] tapply within a data.frame: a simpler alternative?

2008-12-10 Thread Peter Dalgaard
baptiste auguie wrote: Dear list, I have a data.frame with x, y values and a 3-level factor group, say. I want to create a new column in this data.frame with the values of y scaled to 1 by group. Perhaps the example below describes it best: x - seq(0, 10, len=100) my.df - data.frame(x =

Re: [R] tapply within a data.frame: a simpler alternative?

2008-12-10 Thread baptiste auguie
Excellent! I completely forgot its name and existence. Perhaps ave should be mentioned on the help page of either by, tapply, split. Many thanks, baptiste On 10 Dec 2008, at 17:20, Chuck Cleland wrote: On 12/10/2008 12:02 PM, baptiste auguie wrote: Dear list, I have a data.frame with x,

Re: [R] tapply within a data.frame: a simpler alternative?

2008-12-10 Thread baptiste auguie
On 10 Dec 2008, at 17:25, hadley wickham wrote: On Wed, Dec 10, 2008 at 11:02 AM, baptiste auguie [EMAIL PROTECTED] wrote: Dear list, I have a data.frame with x, y values and a 3-level factor group, say. I want to create a new column in this data.frame with the values of y scaled to 1

[R] tapply and any

2008-11-19 Thread Stephan Lindner
Dear all, A quick question which I somehow cannto figure out: I want to apply the function any to subsets of a dataset in order to create a vector with TRUE/FALSE values, depending on whether a subset has the number 5. I.e., y - matrix(c(1,2,3,3,4,5,5,6,6,7,5,1,1,3,5,NA,5,1,1,3),ncol=2) y

Re: [R] tapply and any

2008-11-19 Thread Henrique Dallazuanna
Try this: sapply(tapply(y[,2],na.omit(y[,1]), `==`, 5), any) On Wed, Nov 19, 2008 at 4:47 PM, Stephan Lindner [EMAIL PROTECTED] wrote: Dear all, A quick question which I somehow cannto figure out: I want to apply the function any to subsets of a dataset in order to create a vector with

Re: [R] tapply and any

2008-11-19 Thread Peter Alspach
Stephan Try: tapply(y[,2], y[,1], function(x) any(x==5)) Peter Alspach -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Stephan Lindner Sent: Thursday, 20 November 2008 7:48 a.m. To: [EMAIL PROTECTED] Subject: [R] tapply and any Dear all

[R] tapply versus by in function with more than 1 arguments

2008-10-01 Thread Cézar Freitas
Hi. I searched the list and didn't found nothing similar to this. I simplified my example like below: #I need calculate correlation (for example) between 2 columns classified by a third one at a data.frame, like below: #number of rows nr = 10 #the third column is to enforce that I need

Re: [R] tapply versus by in function with more than 1 arguments

2008-10-01 Thread Henrique Dallazuanna
Try this: sapply(by(dataf[,c(V1,V2)], dataf$class, cor), '[', 3) On Wed, Oct 1, 2008 at 9:21 AM, Cézar Freitas [EMAIL PROTECTED] wrote: Hi. I searched the list and didn't found nothing similar to this. I simplified my example like below: #I need calculate correlation (for example) between

Re: [R] tapply versus by in function with more than 1 arguments

2008-10-01 Thread Gabor Grothendieck
The first tapply in your question subsets V1 but not V2 so they are of different length. To subset both tapply over the row names and perform the subsetting in the function: tapply(rownames(dataf), dataf$class, function(r) cor(dataf[r, V1], dataf[r, V2])) or tapply(rownames(dataf),

Re: [R] tapply versus by in function with more than 1 arguments

2008-10-01 Thread hadley wickham
On Wed, Oct 1, 2008 at 7:21 AM, Cézar Freitas [EMAIL PROTECTED] wrote: Hi. I searched the list and didn't found nothing similar to this. I simplified my example like below: #I need calculate correlation (for example) between 2 columns classified by a third one at a data.frame, like below:

[R] tapply and grouping

2008-05-17 Thread Patrick Hausmann
Hello all, I have a df like this: w - c(1.20, 1.34, 2.34, 3.12, 2.89, 4.67, 2.43, 2.89, 1.99, 3.45, 2.01, 2.23, 1.45, 1.59) g - rep(c(a, b), each=7) df - data.frame(g, w) df # 1. Mean for each group tapply(df$w, df$g, function(x) mean(x)) # 2. Range for each group - fix value 0.15

Re: [R] tapply and grouping

2008-05-17 Thread jim holtman
Is this what you want: w - c(1.20, 1.34, 2.34, 3.12, 2.89, 4.67, 2.43, + 2.89, 1.99, 3.45, 2.01, 2.23, 1.45, 1.59) g - rep(c(a, b), each=7) df - data.frame(g, w) df gw 1 a 1.20 2 a 1.34 3 a 2.34 4 a 3.12 5 a 2.89 6 a 4.67 7 a 2.43 8 b 2.89 9 b 1.99 10 b 3.45 11 b 2.01 12

Re: [R] tapply

2008-04-08 Thread Dimitris Rizopoulos
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: Manuel Montes [EMAIL PROTECTED] To: r-help@r-project.org Sent: Tuesday, April 08, 2008 11:14 AM Subject: [R] tapply

[R] tapply

2008-04-08 Thread Manuel Montes
Does anyone have something like tapply for matrices? I would like to average the contents of columns according to factors but tapply works only with one column at each time. Is something available to do everything in one step? Many thanks. Manuel Montes

Re: [R] tapply

2008-04-08 Thread Chuck Cleland
On 4/8/2008 5:14 AM, Manuel Montes wrote: Does anyone have something like tapply for matrices? I would like to average the contents of columns according to factors but tapply works only with one column at each time. Is something available to do everything in one step? Many thanks.

Re: [R] Tapply for Group Specific Means and Proportions

2008-03-03 Thread jim holtman
Here is how you can get the proportions from your data frame: prop.table(table(paste(x$testdate, x$testtime), x$Behavior),margin=1) EA FL HO MA OS PE SI 28Mar96 1014 0. 0. 0.5000 0. 0.

Re: [R] tapply, mean and subset

2007-11-21 Thread Moshe Olshansky
One possibility is: a- V1 V2 F1 + 1 A 2 0 + 2 A 3 0 + 3 A 4 1 + 4 B 3 0 + 5 B 2 1 + 6 C 6 0 + 7 C 2 0 + 8 C 6 0 + b-read.table(textConnection(a),header=TRUE) f-rep(0,dim(b)[1]) f[b$F1==0] - ave(b$V2[b$F1==0],b$V1[b$F1==0]) cbind(b,f) V1 V2 F1f 1 A 2 0

[R] tapply, mean and subset

2007-11-13 Thread c18g
Dear list, I have this dataframe V1 V2 F1 1 A 2 0 2 A 3 0 3 A 4 1 4 B 3 0 5 B 2 1 6 C 6 0 7 C 2 0 8 C 6 0 and would like to calculate a new column with mean-values, following this rule 1. If F1 = 0 calculate the mean from V2 for each factor in V1. 2. If F1 = 1, then

<    1   2