# How to create a table of frequency for range of values in an R data frame column?

To create a table of frequency for range of values in an R data frame column, we can follow the below steps −

• First of all, create a data frame.
• Then, use table function with cut function to create the table of frequency for range of values.

## Example 1

Create the data frame

Let's create a data frame as shown below −

Live Demo

x<-sample(1:10,20,replace=TRUE)
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 2
2 10
3 8
4 5
5 5
6 2
7 8
8 3
9 5
10 9
11 4
12 10
13 3
14 2
15 7
16 5
17 9
18 9
19 6
20 7

## Create a table of frequency for range of values

Using table function along with cut function to create a table of frequency for range of values say 1 to 11 using column x of data frame df1 −

Live Demo

x<-sample(1:10,20,replace=TRUE)
df1<-data.frame(x)

### Output

(-2,-1.5] (-1.5,-1] (-1,-0.5] (-0.5,0] (0,0.5] (0.5,1] (1,1.5]
2          2          3       4       5       3       1

Updated on: 14-Aug-2021

2K+ Views