# How to find the column mean of first n number of rows in R data frame?

To find the column mean of first n number of rows in R data frame, we can follow the below steps −

• First of all, create a data frame.
• Then, use colMeans function by reading the data frame with matrix function and nrow argument.

## Example1

Create the data frame

Let's create a data frame as shown below −

Live Demo

x<-rnorm(20)
df1<-data.frame(x)
df1

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

      x
1 0.13032553
2 -0.67816114
3 1.42759471
4 -0.38436418
5 -0.61993704
6 1.44777262
7 0.04268727
8 0.48114764
9 -0.27875354
10 0.71026224
11 0.08441343
12 -0.03178720
13 0.69694108
14 -0.57978544
15 2.63039761
16 0.75607995
17 -0.12466369
18 1.03613577
19 -0.26832398
20 1.46741698

## Find the column mean for n number of rows

Using colMeans function to find the column mean after reading the data frame df1 with matrix function for 10 rows −

Live Demo

x<-rnorm(20)
df1<-data.frame(x)

### Output

[1] 7.000000 6.944444
Warning message:
In matrix(df2\$y, nrow = 18) :
data length [20] is not a sub-multiple or multiple of the number of rows [18]