How to create a data frame column with equally spaced values between 0 and 1 in R?


To create a data frame column with equally spaced values between 0 and 1, we can use ppoints function. For example, if we want to create a data frame df with hundred equally spaced values between 0 and 1 then we can use the below mentioned command −

df<-data.frame(x= ppoints(100))

Example 1

Following snippet creates a data frame column with equally spaced values between 0 and 1 −

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

The following dataframe is created −

    x
1  0.025
2  0.075
3  0.125
4  0.175
5  0.225
6  0.275
7  0.325
8  0.375
9  0.425
10 0.475
11 0.525
12 0.575
13 0.625
14 0.675
15 0.725
16 0.775
17 0.825
18 0.875
19 0.925
20 0.975

Example 2

Following snippet creates a data frame column with equally spaced values between 0 and 1 −

y<-ppoints(25)
df2<-data.frame(y)
df2

The following dataframe is created −

     y
1  0.02
2  0.06
3  0.10
4  0.14
5  0.18
6  0.22
7  0.26
8  0.30
9  0.34
10 0.38
11 0.42
12 0.46
13 0.50
14 0.54
15 0.58
16 0.62
17 0.66
18 0.70
19 0.74
20 0.78
21 0.82
22 0.86
23 0.90
24 0.94
25 0.98

Example 3

Following snippet creates a data frame column with equally spaced values between 0 and 1 −

z<-ppoints(30)
df3<-data.frame(z)
df3

The following dataframe is created −

    z
1  0.01666667
2  0.05000000
3  0.08333333
4  0.11666667
5  0.15000000
6  0.18333333
7  0.21666667
8  0.25000000
9  0.28333333
10 0.31666667
11 0.35000000
12 0.38333333
13 0.41666667
14 0.45000000
15 0.48333333
16 0.51666667
17 0.55000000
18 0.58333333
19 0.61666667
20 0.65000000
21 0.68333333
22 0.71666667
23 0.75000000
24 0.78333333
25 0.81666667
26 0.85000000
27 0.88333333
28 0.91666667
29 0.95000000
30 0.98333333

Updated on: 11-Nov-2021

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements