I know this question is very simple, but I am not figure it out I have the data frame: test<- data.frame(year=c(2000,2000,2001,2001),x=c(54,41,90,15), y=c(29,2,92,22), z=c(26,68,46,51)) test year x y z 1 2000 54 29 26 2 2000 41 2 68 3 2001 90 92 46 4 2001 15 22 51 I want to sum the vectors x, y and z within each year (2000 and 2001) to obtain this: year x y z 1 2000 95 31 94 2 2001 105 114 97 I tried tapply but did not work (or probably I do it wrong) Any suggestions? silva [[alternative HTML version deleted]]
On Thu, 2005-04-21 at 16:31 +0100, jose silva wrote:> I know this question is very simple, but I am not figure it out> I have the data frame:> test<- data.frame(year=c(2000,2000,2001,2001),x=c(54,41,90,15), y=c(29,2,92,22), z=c(26,68,46,51))> test> year x y z> 1 2000 54 29 26> 2 2000 41 2 68> 3 2001 90 92 46> 4 2001 15 22 51> I want to sum the vectors x, y and z within each year (2000 and 2001) to obtain this:> > year x y z> 1 2000 95 31 94> 2 2001 105 114 97> I tried tapply but did not work (or probably I do it wrong)> > Any suggestions?tapply() is typically used against a single vector, subsetting by one or more factors. In this case, since you want to get the colSums for more than one column in the data frame, there are a few options: 1. Use by():> by(test[, -1], test$year, colSums)test$year: 2000 x y z 95 31 94 ------------------------------------------------------ test$year: 2001 x y z 105 114 97 2. Use aggregate():> aggregate(test[, -1], list(Year = test$year), sum)Year x y z 1 2000 95 31 94 2 2001 105 114 97 3. Use split() and then lapply():> test.s <- split(test, test$year) > test.s$"2000" year x y z 1 2000 54 29 26 2 2000 41 2 68 $"2001" year x y z 3 2001 90 92 46 4 2001 15 22 51> lapply(test.s, function(x) colSums(x[, -1]))$"2000" x y z 95 31 94 $"2001" x y z 105 114 97 Which you choose may depend upon how you need the output structured for subsequent use. See ?by, ?aggregate, ?lapply and ?split for more information. HTH, Marc Schwartz
sapply(split(test, test$year), function(x) list(x.s = sum(x$x), y.s = sum(x$y), z.s = sum(x$z))) or for one variable only aggregate(test$x, list(id = test$year), sum) cheers christoph jose silva wrote:> I know this question is very simple, but I am not figure it out > > > > > I have the data frame: > > > > > > > > > > test<- data.frame(year=c(2000,2000,2001,2001),x=c(54,41,90,15), y=c(29,2,92,22), z=c(26,68,46,51)) > > > > > test > > > > > year x y z > > > > > 1 2000 54 29 26 > > > > > 2 2000 41 2 68 > > > > > 3 2001 90 92 46 > > > > > 4 2001 15 22 51 > > > > > > > > > > I want to sum the vectors x, y and z within each year (2000 and 2001) to obtain this: > > > > > > > > > > year x y z > > > > > 1 2000 95 31 94 > > > > > 2 2001 105 114 97 > > > > > > > > > > I tried tapply but did not work (or probably I do it wrong) > > > > > > > > > > Any suggestions? > > > > > > > > > > silva > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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 > >
Hi all -- I'm running a Factor Analysis on my dataset, and I've located the "factanal()" and "princomp()" methods. I don't want to do a PCA, so it looks like I should use factanal(), but factanal() requires specifying the number of factors you expect from the analysis. Are there any packages out there explicitly for Exploratory Factor Analysis that do not require specifying the number of expected factors? -- Chris
hi all, In the library ‘ade4’, there are two eigenanalysis which enable the ordination of the categorical variables. 1- Multiple Correspondence Analysis (MCA, Tenenhaus & Young 1985) performs the multiple correspondence analysis of a factor table (see the function ‘dudi.acm’). this function is equivalent to functions mca of the library(MASS) 2- the “mixed factorial analysis” (Hill & Smith 1976) enables the ordination of tables mixing quantitative variables and factors (functions ‘dudi.mix’ or ‘dudi.hillsmith’). I hope this helps, P.BADY At 15:47 21/04/2005 -0500, Chris Bergstresser wrote:>Hi all -- > > I'm running a Factor Analysis on my dataset, and I've located the > "factanal()" and "princomp()" methods. I don't want to do a PCA, so it > looks like I should use factanal(), but factanal() requires specifying > the number of factors you expect from the analysis. > Are there any packages out there explicitly for Exploratory Factor > Analysis that do not require specifying the number of expected factors? > >-- Chris > >______________________________________________ >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.htmlPierre BADY <°)))))>< Université Claude Bernard Lyon 1 UMR CNRS 5023, LEHF bat Alphonse Forel 43 boulevard du 11 novembre 1918 F-69622 VILLEURBANNE CEDEX FRANCE TEL : +33 (0)4 72 44 62 34 FAX : +33 (0)4 72 43 28 92 MEL : pierre.bady@univ-lyon1.fr http://limnologie.univ-lyon1.fr http://pierre.bady.free.fr (in construction) [[alternative HTML version deleted]]