R – How to calculate mean vector of data set in R

meanobjectrvector

I'm very new to R, so please bear with me.

Given test data in a .txt file…

Num   height   weight   class  
1     72.1     147.5    1  
2     75.2     125.4    1
3     60.3     102.3    2
4     62.4     98.2     2
5     50.5     74.1     3

I read the file into R by:

testdata <- read.table("testdata.txt", header = TRUE)

which creates the data with 15 obs. of 4 variables. I can assign each column to an object, like so:

numCol <- testdata$Num
heightCol <- testdata$height
weightCol <- testdata$weight
classCol <- testdata$class

Then, for instance, heightCol gives me

heightCol
> 72.1 75.2 60.3 62.4 50.5

What I need to do is take each class and find the mean vectors, standard deviation, scatter diagrams, and parallel coordinates.

If someone could explain to me how to find, say, just the mean vectors I'm sure I could figure out the rest.

Best Answer

colMeans(testdata)

or

apply(testdata, 2, mean)

for more details see the helpfiles

?colMeans
?apply