[R] unable to force the vector format
Charles Plessy
charles-r-nospam at plessy.org
Wed Dec 14 14:12:16 CET 2005
Dear all,
I am so ashamed to pollute the list with a trivial question, but it is a
long time I have not used R, and I need a result in the next one or two
hour...
I have a table which I have loaded with read.table, and I want to make
the mean of its columns.
> slides <- read.table("slides.txt")
> slides [1:5,]
V1 V2 V3 V4 V5 V6 V7 V8
1 PLB00090AA02 0.147 0.018 0.046 0.064 -0.018 -0.008 -0.063
2 PLB00090BC08 0.171 0.011 -0.001 0.009 0.052 0.032 -0.065
3 PLB00090CG02 0.029 -0.014 -0.042 0.006 0.024 -0.009 -0.043
4 PLB00091AA08 0.033 0.050 -0.022 -0.002 0.038 0.015 -0.037
5 PLB00091BE02 0.183 0.039 0.052 -0.014 -0.034 -0.037 0.037
but I can not get the mean :
> mean(slides [1,2:8])
V2 V3 V4 V5 V6 V7 V8
0.147 0.018 0.046 0.064 -0.018 -0.008 -0.063
obviously, I fail to tell R that I am using a vector.
> y<- c(1,2,3,4)
> mean(y)
[1] 2.5
but as.vector does not solve my problem
> lapply(as.vector(slides[1,2:8]),sum)
$V2
[1] 0.147
$V3
[1] 0.018
$V4
[1] 0.046
$V5
[1] 0.064
$V6
[1] -0.018
$V7
[1] -0.008
$V8
[1] -0.063
In the end, I would like to use lapply to fill a new column in the table with the means.
(and then extract the closest ones to zero...)
Once again, sorry for this mail, whose answer is probably trivial, but it would
be an enormous help if somebody could sent it to me!
--
Charles
More information about the R-help
mailing list