[R] Odp: Multiplication of data frame with vector

Petr PIKAL petr.pikal at precheza.cz
Tue Jul 7 10:36:00 CEST 2009


Hi

r-help-bounces at r-project.org napsal dne 07.07.2009 10:05:09:

> 
> Dear group:
> sorry for my beginners question, but I'm rather new to R and was 
searching 
> high and low without success:
> 
> I have a data frame (df) with variables in the rows and observations in 
the 
> columns like (the actual data frame has 15 columns and 1789 rows):
> 
>           early1 early2 early3 early4 early5
> M386T1000  57056  55372  58012  55546  57309
> M336T90    11063  10312  10674  10840  11208
> M427T91    12064  11956  12692  12340  11924
> M429T91     4594   3890   4096   4019   4204
> M447T90    26553  27647  26889  26751  26929
> 
> Now I'm trying to transform each value column-wise to make columns to 
all have
> the same mean with:
> 
> df * mean(mean(df)) / mean(df).
> 
> I just can't get my head around this: mean(df) gives me the correct 
column 
> means vector, and mean(mean(df)) gives me the correct total mean. The 
above 
> operation works correctly for individual rows, i.e. if I do
>  df[1,]*mean(mean(df))/mean(df)

a little bit awkward

t(t(as.matrix(df))*mean(mean(df))/mean(df))

or with apply
ttt<-mean(mean(df))/mean(df)
t(apply(df, 1, function(x) x*ttt))

There could be some sapply version of it but at present time I can not 
find it.
Regards
Petr

> 
> Can someone tell me what I am doing wrong??
> Thanks!
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list