# Example data frame
my_data <- dummy_data(1000)
# Get row numbers
my_data[["run_nr"]] <- my_data |> running_number()
# Running number per variable expression
my_data[["run_nr_by"]] <- my_data |> running_number(by = year)
# Mark first and last cases
my_data[["first"]] <- my_data |> mark_case(by = household_id)
my_data[["last"]] <- my_data |> mark_case(by = household_id,
first = FALSE)
# Retain first value without grouping
my_data[["first_weight"]] <- my_data |> retain_value(value = weight)
# Retain first value inside a group for multiple variables
my_data[, c("household_weight", "household_icome")] <- my_data |>
retain_value(values = c(weight, income),
by = c(state, household_id))
# Retain sum without grouping
my_data[["total_sum"]] <- my_data |> retain_sum(values = weight)
# Retain sum inside a group for multiple variables
my_data[, c("weight_sum", "income_sum")] <- my_data |>
retain_sum(values = c(weight, income),
by = c(state, household_id))
# Retain columns inside data frame, which orders them to the front
my_data <- my_data |> retain_variables(age, sex, income)
# Retain columns inside data frame, but order them to the end.
# Variable ranges can also be used.
my_data <- my_data |> retain_variables(age:income, order_last = TRUE)
# Retain columns inside data frame and add new variables with all NA values
my_data <- my_data |> retain_variables(age, sex, income, status1:status5)
# You can also use the colon as a placeholder for any text
start1 <- my_data |> retain_variables("s:") # Variable names start with "s"
end1 <- my_data |> retain_variables(":id") # Variable names end with "id"
contain1 <- my_data |> retain_variables(":on:") # Variable names which contain "on"
Run the code above in your browser using DataLab