Skip to content

basic question

6 messages · jose silva, Marc Schwartz, Christoph Lehmann +2 more

#
On Thu, 2005-04-21 at 16:31 +0100, jose silva wrote:
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():
test$year: 2000
 x  y  z
95 31 94
------------------------------------------------------
test$year: 2001
  x   y   z
105 114  97



2. Use aggregate():
Year   x   y  z
1 2000  95  31 94
2 2001 105 114 97




3. Use split() and then lapply():
$"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
$"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)
2000 2001
x   95  105
y   31  114
z   94   97

or transpose using t():
x   y  z
2000  95  31 94
2001 105 114 97


which is similar of course to the use of aggregate():
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:
#
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