dfs = data.frame(
test = 1:5,
a = rep(10, 5),
b_1 = rep(11, 5),
b_2 = rep(12, 5),
b_3 = rep(13, 5),
b_4 = rep(14, 5),
b_5 = rep(15, 5)
)
# compute sum of b* variables and attach it to 'dfs'
let(dfs,
b_total = sum_row(b_1 %to% b_5),
b_total = set_var_lab(b_total, "Sum of b"),
random_numbers = runif(.N) # .N usage
) %>% print()
# calculate sum of b* variables and return it
query(dfs, sum_row(b_1 %to% b_5))
# set values to existing/new variables
let(dfs,
columns('new_b{1:5}') := b_1 %to% b_5
) %>% print()
# conditional modification
let_if(dfs, test %in% 2:4,
a = a + 1,
b_total = sum_row(b_1 %to% b_5),
random_numbers = runif(.N) # .N usage
) %>% print()
# variable substitution
name1 = "a"
name2 = "new_var"
let(dfs,
(name2) := get(name1)*2
) %>% print()
# 'use_labels' examples. Utilization of labels in base R.
data(mtcars)
mtcars = apply_labels(mtcars,
mpg = "Miles/(US) gallon",
cyl = "Number of cylinders",
disp = "Displacement (cu.in.)",
hp = "Gross horsepower",
drat = "Rear axle ratio",
wt = "Weight (lb/1000)",
qsec = "1/4 mile time",
vs = "Engine",
vs = c("V-engine" = 0,
"Straight engine" = 1),
am = "Transmission",
am = c("Automatic" = 0,
"Manual"=1),
gear = "Number of forward gears",
carb = "Number of carburetors"
)
use_labels(mtcars, table(am, vs))
if (FALSE) {
use_labels(mtcars, plot(mpg, hp))
}
mtcars %>%
use_labels(lm(mpg ~ disp + hp + wt)) %>%
summary()
Run the code above in your browser using DataLab