forked from matthieugomez/statar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.Rmd
executable file
·46 lines (36 loc) · 1.21 KB
/
vector.Rmd
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
---
title: "statar"
author: "Matthieu Gomez"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Summary function}
%\VignetteEngine{knitr::rmarkdown}
%\usepackage[utf8]{inputenc}
---
## Vector functions
The package adds the following vector functions
```R
# count returns the number of non missing observation
count(c(NA, NA, 1), 1:3)
count(data_frame(c(NA, NA, 1), 1:3))
# sample_mode returns the statistical mode
sample_mode(c(1, 2, 2))
sample_mode(c(1, 2))
sample_mode(c(NA, NA, 1))
sample_mode(c(NA, NA, 1), na.rm = TRUE)
# pctile computes quantile and weighted quantile of type 2 (similarly to Stata _pctile)
v <- c(NA, 1:10)
pctile(v, probs = c(0.3, 0.7), na.rm = TRUE)
# xtile creates integer variable for quantile categories (corresponds to Stata xtile)
v <- c(NA, 1:10)
xtile(v, n_quantiles = 3) # 3 groups based on terciles
xtile(v, probs = c(0.3, 0.7)) # 3 groups based on two quantiles
xtile(v, cutpoints = c(2, 3)) # 3 groups based on two cutpoints
# winsorize (default based on 5 x interquartile range)
v <- c(1:4, 99)
winsorize(v)
winsorize(v, replace = NA)
winsorize(v, probs = c(0.01, 0.99))
winsorize(v, cutpoints = c(1, 50))
```