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

R ProgrammingServer Side ProgrammingProgramming

#### Artificial Intelligence : The Future Of Programming

15 Lectures 54 mins

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

To find the column totals 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 totals if some 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 totals if some columns are categorical

Using numcolwise function from plyr package to find the column totals 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(sum)(df1)

## Output

  Score Frequency
1 627    81

## 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 totals if some columns are categorical

Using numcolwise function from plyr package to find the column totals 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(sum)(df2)

## Output

   v1 v2
1 56  147
Updated on 12-Nov-2021 06:51:05