Reputation: 25
I have a series of variables with values 1.2, 2.5 etc. I would like to separate the numbers by decimal, so that I create a new column for the whole number and the decimal point, and then assign a total score.
HT_Q1 <- c(1.2, 2.5, 7.4)
HT_Q2 <- c(2.5, 8.5, 9.5)
AT_Q1 <- c(2.4, 1.2, 1.4)
AT_Q2 <- c(6.5, 1.5, 9.10)
df <- data.frame(HT_Q1, HT_Q2, AT_Q1, AT_Q2)
I can do this using mutate:
mutate(df,
HT_Q1_G = trunc(HT_Q1),
HT_Q1_B = HT_Q1 %% 1 * 10,
HT_Q1_P = (HT_Q1_G * 6) + HT_Q1_B)
However, I would like to write a function so I don't have to repeat the above code for each variable. Is it possible to pass each variable (HT_Q1, HT_Q2 etc.) as an argument to the function and create the corresponding variables (e.g. HT_Q2_G, HT_Q2_B, AT_Q2_G etc.)?
I have tried to create variable names based on the argument I pass to the function but it does not work:
edit_score <- function(var){
mutate(df,
paste0(var, "_G") = trunc(var),
paste0(var, "_B") = var %% 1 * 10,
paste0(var, "_P") = (paste0(var, "_G") * 6) + paste0(var, "_B"))
}
edit_score(HT_Q1)
edit_score(HT_Q2)
edit_score(AT_Q1)
edit_score(AT_Q2)
I am new to R and come from a SAS background where I am used to using the macro compiler to adjust text in code before it is executed.
Upvotes: 2
Views: 695
Reputation: 35584
You can use across()
with the feature of lst()
that refers to components created earlier.
library(dplyr)
df %>%
mutate(across(.fns = lst( G = function(x) trunc(x),
B = function(x) x %% 1 * 10,
P = ~ (G(.) * 6) + B(.) )))
Output
across()
automatically creates new column names separated by "_"
as you want. You can also customize a new name pattern by the .names
argument.
# HT_Q1 HT_Q2 AT_Q1 AT_Q2 HT_Q1_G HT_Q1_B HT_Q1_P
# 1 1.2 2.5 2.4 6.5 1 2 8
# 2 2.5 8.5 1.2 1.5 2 5 17
# 3 7.4 9.5 1.4 9.1 7 4 46
#
# HT_Q2_G HT_Q2_B HT_Q2_P AT_Q1_G AT_Q1_B AT_Q1_P
# 1 2 5 17 2 4 16
# 2 8 5 53 1 2 8
# 3 9 5 59 1 4 10
#
# AT_Q2_G AT_Q2_B AT_Q2_P
# 1 6 5 41
# 2 1 5 11
# 3 9 1 55
Upvotes: 1
Reputation: 9865
# python-like string concatenation `+`
`%+%` <- function(str1, str2) {
paste0(str1, str2)
}
add_columns <- function(df, col) {
df[, col %+% "_G"] <- trunc(df[, col])
df[, col %+% "_B"] <- df[, col] %% 1 * 10
df[, col %+% "_P"] <- df[, col %+% "_G"] * 6 + df[, col %+% "_B"]
df
}
generate_GBP_columns <- function(df) {
for (col in names(df)) {
df <- add_columns(df, col)
}
df
}
generate_GBP_columns(df)
# HT_Q1 HT_Q2 AT_Q1 AT_Q2 HT_Q1_G HT_Q1_B HT_Q1_P HT_Q2_G HT_Q2_B HT_Q2_P
# 1 1.2 2.5 2.4 6.5 1 2 8 2 5 17
# 2 2.5 8.5 1.2 1.5 2 5 17 8 5 53
# 3 7.4 9.5 1.4 9.1 7 4 46 9 5 59
# AT_Q1_G AT_Q1_B AT_Q1_P AT_Q2_G AT_Q2_B AT_Q2_P
# 1 2 4 16 6 5 41
# 2 1 2 8 1 5 11
# 3 1 4 10 9 1 55
Upvotes: 0
Reputation: 1178
When using tidyverse, it's important to have your data in a tidy, long format. This makes using the tidyverse functions a lot easier. Using the gather function, we can convert your data to a long format and mutate will apply the functions to all values.
HT_Q1 <- c(1.2, 2.5, 7.4)
HT_Q2 <- c(2.5, 8.5, 9.5)
AT_Q1 <- c(2.4, 1.2, 1.4)
AT_Q2 <- c(6.5, 1.5, 9.10)
df <- data.frame(HT_Q1, HT_Q2, AT_Q1, AT_Q2)
df <- df %>%
gather() %>%
mutate(G = trunc(value),
B = value %% 1 * 10,
P = G*6 + B)
# key value G B P
#1 HT_Q1 1.2 1 2 8
#2 HT_Q1 2.5 2 5 17
#3 HT_Q1 7.4 7 4 46
#4 HT_Q2 2.5 2 5 17
#5 HT_Q2 8.5 8 5 53
#6 HT_Q2 9.5 9 5 59
#7 AT_Q1 2.4 2 4 16
#8 AT_Q1 1.2 1 2 8
#9 AT_Q1 1.4 1 4 10
#10 AT_Q2 6.5 6 5 41
#11 AT_Q2 1.5 1 5 11
#12 AT_Q2 9.1 9 1 55
If you really want to go back to wide format, though not recommended, you can pivot back with the following:
df <- df %>%
pivot_wider(id_cols = key, names_from = key, values_from = value:P, values_fn=list, , names_glue = "{key}_{.value}") %>%
unnest(cols=everything())
colnames(df) = gsub("_value", "", colnames(df))
# HT_Q1 HT_Q2 AT_Q1 AT_Q2 HT_Q1_G HT_Q2_G AT_Q1_G AT_Q2_G HT_Q1_B HT_Q2_B AT_Q1_B AT_Q2_B HT_Q1_P HT_Q2_P AT_Q1_P AT_Q2_P
#1 1.2 2.5 2.4 6.5 1 2 2 6 2 5 4 5 8 17 16 41
#2 2.5 8.5 1.2 1.5 2 8 1 1 5 5 2 5 17 53 8 11
#3 7.4 9.5 1.4 9.1 7 9 1 9 4 5 4 1 46 59 10 55
Upvotes: 1
Reputation: 389012
You can use non-standard evaluation here :
library(dplyr)
library(purrr)
library(rlang)
edit_score <- function(var){
transmute(df,
!!paste0(var, "_G") := trunc(!!sym(var)),
!!paste0(var, "_B") := !!sym(var) %% 1 * 10,
!!paste0(var, "_P") := !!sym(paste0(var, "_G")) * 6 +
!!sym(paste0(var, "_B")))
}
bind_cols(df, map_dfc(names(df), edit_score))
sym
converts character value of column name to symbol and !!
is used to evaluate it.
Non-standard evaluation can be difficult to understand initially, in such case you can also use this base R approach :
edit_score <- function(var){
col1 <- paste0(var, "_G")
col2 <- paste0(var, "_B")
col3 <- paste0(var, "_P")
df[[col1]] <- trunc(df[[var]])
df[[col2]] <- df[[var]] %% 1 * 10
df[[col3]] <- df[[col1]] * 6 + df[[col2]]
df[, c(col1, col2, col3)]
}
cbind(df, do.call(cbind, lapply(names(df), edit_score)))
Upvotes: 2