Stata | R |
---|---|
max(v1) | max(v1) |
min(v1) | min(v1) |
max(v1, v2) | pmax(v1, v2) |
min(v1, v2) | pmin(v1, v2) |
cond(v1 > 3, 0, 1) | ifelse(v1 > 3, 0, 1) |
nvals | n_distinct |
group | interaction |
tag | row_number() == 1 |
The package statar
also includes functions that correspond to the Stata commands count
, mode
, xtile
and _pctile
Use backlash to escape quotes in a character:
print("Package \"dplyr\"")
#> [1] "Package \"dplyr\""
Automatically escape double quotes within a string by enclosing it within single quotes (similarly to Stata compound quotes):
# Stata: display(`"Package "dplyr""')
print('Package "dplyr"')
#> [1] "Package \"dplyr\""
Misc functions:
Stata | R |
---|---|
lower | tolower |
upper | toupper |
trim | str_strim |
strlen | str_length |
substr | str_sub |
Combine strings with paste
filename <- "temp.csv"
filepath <- paste("~/Dropbox", filename, sep = "")
df <- read_csv(filepath)
Note the difference between the option sep
and collapse
paste("x1", "x2", "x3", sep = "+")
#> [1] "x1+x2+x3"
paste(c("x1", "x2", "x3"), collapse = "+")
#> [1] "x1+x2+x3"
Use the package stringr
to match, replace, or split strings according to certain patterns. Each function of stringr
has the same syntax: the first argument is a character vector, the second argument is a regex pattern. Instead of a regex pattern, use fixed()
for exact matching, or glob2rx
for wildcard matching.
stringr | Stata (fixed) | Stata (wildcard) | Stata (regex) |
---|---|---|---|
str_detect | strmatch | regexm | |
str_locate | strpos | ||
str_match | regexs if regexm | ||
str_replace | subinstr(,1) | regexr | |
str_replace_all | subinstr(,.) | ||
str_split | split |
Because backlashes are used to escape quotes, backlashes themselves should be escaped in a regex pattern. For instance, to suppress the first dot in a string:
# Stata: regexr("Package .", "\.","")
str_replace("Package .","\\.", "")
Compute distances between strings with the package stringdist
.
Use the package lubridate
to work with dates. To convert strings (of daily date) and integers (number of days with respect to origin) to daily dates, one can use the function ymd
, mdy
, etc:
library(lubridate)
ymd("1900-01-01")
mdy("03/01/1998")
Extract information on dates by using the lubridate function week
, month
, year
x <- mdy("03/01/1998")
week(x)
month(x)
year(x)
Add periods to date through the function weeks
, months
and years
x + days(1)
x + weeks(1)
x + months(1)
Round dates using the function floor_date
or ceiling_date
floor_date(x, unit = "week")
The package statar
defines classes for monthly, and quarterly dates, coded as elapsed periods since “01/01/190”. This allows to use simple arithmetic on quarters and months, similarly to Stata.
datem <- as.monthly(x)
datem
#> [1] "1998m2"
datem + 1
#> [1] "1998m3"