quantcut
2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
quantcut package:gtools R Documentation
_C_r_e_a_t_e _a _F_a_c_t_o_r _V_a_r_i_a_b_l_e _U_s_i_n_g _t_h_e _Q_u_a_n_t_i_l_e_s _o_f _a _C_o_n_t_i_n_u_o_u_s _V_a_r_i_a_b_l_e
_D_e_s_c_r_i_p_t_i_o_n:
Create a factor variable using the quantiles of a continous
variable.
_U_s_a_g_e:
quantcut(x, q=seq(0,1,by=0.25), na.rm=TRUE, ...)
_A_r_g_u_m_e_n_t_s:
x: Continous variable.
q: Vector of quantiles used for creating groups. Defaults to
'seq(0, 1, by=0.25)'. See 'quantile' for details.
na.rm: Boolean indicating whether missing values should be removed
when computing quantiles. Defaults to TRUE.
...: Optional arguments passed to 'cut'.
_D_e_t_a_i_l_s:
This function uses 'quantile' to obtain the specified quantiles of
'x', then calls 'cut' to create a factor variable using the
intervals specified by these quantiles.
It properly handles cases where more than one quantile obtains the
same value, as in the second example below. Note that in this
case, there will be fewer generated factor levels than the
specified number of quantile intervals.
_V_a_l_u_e:
Factor variable with one level for each quantile interval given by
'q'.
_A_u_t_h_o_r(_s):
Gregory R. Warnes warnes@bst.rochester.edu
_S_e_e _A_l_s_o:
'cut', 'quantile'
_E_x_a_m_p_l_e_s:
## create example data
x <- rnorm(1000)
## cut into quartiles
quartiles <- quantcut( x )
table(quartiles)
## cut into deciles
deciles <- quantcut( x, seq(0,1,by=0.1) )
table(deciles)
## show handling of 'tied' quantiles.
x <- round(x) # discretize to create ties
stem(x) # display the ties
deciles <- quantcut( x, seq(0,1,by=0.1) )
table(deciles) # note that there are only 5 groups (not 10)
# due to duplicates