Hi,

I have the data set bellow: where the first column represent the row
names, the second column "x", is the variable that I am trying to
process, and the third column "factor" contains ids for packages of the
variable x, see part 3 bellow.


I have written a function (which I tested and seems to work) that
performs my desired process on "x". The function returns a table of
transformation on  the variable x. I am using the function "tapply" to
generate results for each package of the column "factors",  see part 2
bellow.


The code seems to generate correct results. See output for factors '92'
and '106' on part 1 bellow.

My question is how do I instruct the function "tapply" to display the
ROWNAMES associated with each row and for each factor?  I read the
?tapply, but I am not clear how to implement the instructions. 

I appreciate any help.


Thanks,

Jorge Nieves



PART 1
$`92`
       res1     res2     res3
 [1,]  1156 82.80097  0.00000
 [2,]  1225  0.00000  0.00000
 [3,]   625  0.00000  0.00000
 [4,] 15876  0.00000  0.00000
 [5,] 35344  0.00000  0.00000
 [6,]  1089  0.00000  0.00000
 [7,]   196  0.00000  0.00000
 [8,]   324  0.00000  0.00000
 [9,]  4489  0.00000 24.66667

$`106`
       res1     res2  res3
 [1,]  1296 88.45595   0.0
 [2,]  3844  0.00000   0.0
 [3,]  1764  0.00000   0.0
 [4,]   676  0.00000   0.0
 [5,] 12321  0.00000   0.0
 [6,]  5929  0.00000   0.0
 [7,] 42025  0.00000   0.0
 [8,]  1089  0.00000   0.0
 [9,]  3249  0.00000   0.0
[10,]  3844  0.00000 -23.7




PART 2
yy <-  mydata[,"x"]
myfactor <- mydata[,"factor"]


mytest <- tapply(yy,myfactor,myfunction)
mytest


myfunction <- function(yy)
{
 np = length(yy)
 r.names = rownames(yy)
 c.names = c("res1","res2","res3")
 mytable = matrix(0,np,length(c.names),dimnames = list(r.names,c.names))

 mytable[,"res1"] = yy^2
 mytable[1,"res2"] = sd(yy) 
 mytable[np,"res3"] = mean(yy)
 return(mytable)
}

PART 3
> mydata
                x      factor
20080927  -76      0
20080926  162      0
20080925 -120      0
20080924   73      0
20080923   17     20
20080922  -83     20
20080921   40     20
20080920 -131     20
20080919   76     20
20080918   62     50
20080917  -56     50
20080916  118     50
20080915   -8     50
20080914   97     50
20080913   72     50
20080912  -14     50
20080911 -158     50
20080910 -304      0
20080909 -180     75
20080908  -75     75
20080907   34     92
20080906   35     92
20080905   25     92
20080904 -126     92
20080903  188     92
20080902  -33     92
20080901   14     92
20080900   18     92
20080899   67     92
20080898    0      0
20080897   36    106
20080896  -62    106
20080895  -42    106
20080894  -26    106
20080893  111    106
20080892  -77    106
20080891 -205    106
20080890   33    106
20080889   57    106
20080888  -62    106
> 

	[[alternative HTML version deleted]]

