An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20050421/7a64d73d/attachment.pl
basic question
6 messages · jose silva, Marc Schwartz, Christoph Lehmann +2 more
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
<snip> Oops...I forgot one more, using 'test.s' as per the prior e-mail: test.s <- split(test, test$year)
sapply(test.s, function(x) colSums(x[, -1]))
2000 2001 x 95 105 y 31 114 z 94 97 or transpose using t():
t(sapply(test.s, function(x) colSums(x[, -1])))
x y z 2000 95 31 94 2001 105 114 97 which is similar of course to the use of aggregate():
aggregate(test[, -1], list(Year = test$year), sum)
Year x y z 1 2000 95 31 94 2 2001 105 114 97 The key difference is that aggregate() returns a data frame, whereas sapply() returns a matrix in this case. So, see ?sapply as well. 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
An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20050422/797f90c7/attachment.pl