How to find the column means if some columns are categorical in R data frame?

R ProgrammingServer Side ProgrammingProgramming

To find the column means if some columns are categorical in R data frame, we can follow the below steps −

  • First of all, create a data frame.

  • Then, use numcolwise function from plyr package to find the column means ifsome columns are categorical.

Example 1

Create the data frame

Let’s create a data frame as shown below −

Grp<-sample(c("Male","Female"),25,replace=TRUE)
Score<-sample(1:50,25)
Frequency<-sample(1:5,25,replace=TRUE)
df1<-data.frame(Grp,Score,Frequency)
df1

Output

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

   Grp    Score Frequency
1  Male    22     2
2  Male    10     2
3  Female  38     4
4  Female  13     2
5  Male    24     5
6  Male    39     2
7  Male    23     5
8  Female  42     3
9  Female  15     4
10 Female  49     4
11 Male    18     2
12 Female  30     5
13 Male    17     2
14 Male     4     3
15 Male    34     2
16 Female   3     5
17 Male    35     2
18 Female  31     3
19 Female  43     5
20 Female   9     4
21 Female  12     4
22 Female   8     4
23 Male    29     2
24 Male    46     3
25 Male    33     2

Find the column means if some columns are categorical

Using numcolwise function from plyr package to find the column means of numerical columns in the data frame df1 −

Grp<-sample(c("Male","Female"),25,replace=TRUE)
Score<-sample(1:50,25)
Frequency-<sample(1:5,25,replace=TRUE)
df1<-data.frame(Grp,Score,Frequency)
library(plyr)
numcolwise(mean)(df1)

Output

   Score Frequency
1 25.08  3.24

Example 2

Create the data frame

Let’s create a data frame as shown below −

factor<-sample(LETTERS[1:4],25,replace=TRUE)
v1<-rpois(25,2)
v2<-rpois(25,5)
df2<-data.frame(factor,v1,v2)
df2

Output

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

   factor v1 v2
1  C      3  7
2  C      2  5
3  C      2  5
4  A      3  7
5  C      4  8
6  B      0  6
7  C      3  6
8  D      3  4
9  D      2  4
10 A      6  10
11 A      2  4
12 A      2  5
13 B      3  6
14 C      2  7
15 A      2  4
16 C      0  6
17 C      3  7
18 B      1  7
19 C      1  6
20 B      2  3
21 A      1  8
22 C      1  1
23 A      3  7
24 A      4  7
25 B      1  7

Find the column means if some columns are categorical

Using numcolwise function from plyr package to find the column means of numerical columns in the data frame df2 −

factor<-sample(LETTERS[1:4],25,replace=TRUE)
v1<-rpois(25,2)
v2<-rpois(25,5)
df2<-data.frame(factor,v1,v2)
library(plyr)
numcolwise(mean)(df2)

Output

    v1  v2
1 2.24 5.88
raja
Published on 08-Nov-2021 11:21:39

Advertisements