Summaries and Subscripting

Let’s suppose we’ve collected some data from an experiment and stored them in an object x:

> x<-c(7.5,8.2,3.1,5.6,8.2,9.3,6.5,7.0,9.3,1.2,14.5,6.2)

Some simple summary statistics of these data can be produced:

> mean(x)

[1] 7.216667

> var(x)

[1] 11.00879

> summary(x)

Min. 1st Qu. Median Mean 3rd Qu. Max.

1.200 6.050 7.250 7.217 8.475 14.500

which should all be self explanatory. It may be, however, that we subsequently learn that the first 6 data correspond to measurements made on one machine, and the second six on another machine. This might suggest summarizing the two sets of data separately, so we would need to extract from x the two relevant sub-vectors. This is achieved by subscripting:

> x[1:6]


> x[7:12]

give the relevant sub-vectors. Hence,

> summary(x[1:6])

Min. 1st Qu. Median Mean 3rd Qu. Max.

3.100 6.075 7.850 6.983 8.200 9.300

> summary(x[7:12])

Min. 1st Qu. Median Mean 3rd Qu. Max.

1.200 6.275 6.750 7.450 8.725 14.500

Other subsets can be created in the obvious way. For example:

> x[c(2,4,9)] [1] 8.2 5.6 9.3

Negative integers can be used to exclude particular elements. For example x[-(1:6)] has the same effect as x[7:12].

Get industry recognized certification – Contact us