join datasets using a quosure as the by argument
The c()
function doesn't support the rlang bangs so you'll have to take a more traditional approach to building your parameter. You can do
join_by_quosure <- function(data, left_index, var_to_impute, right_index){
require(dplyr)
left_index <- enquo(left_index)
right_index <- enquo(right_index)
var_to_impute <- enquo(var_to_impute)
by = set_names(quo_name(right_index), quo_name(left_index))
left_join(data,
data %>% select(!!right_index, !!var_to_impute),
by = by)
}
Joining two data sets using as_label / as_name instead of quo_name within dplyr's left_join
An option would to convert to sym
bol and then to string with as_string
clean_and_join <-
function(data_one,
data_two,
column_id_one,
column_id_two,
col_nums_one,
col_nums_two) {
clean_data_one <- filter(data_one, {{col_nums_one}} %% 2 == 0)
clean_data_two <- filter(data_two, {{col_nums_two}} %% 2 != 0)
by_cols <- set_names(rlang::as_string(rlang::ensym(column_id_one)),
rlang::as_string(rlang::ensym(column_id_two)))
left_join(
x = clean_data_one,
y = clean_data_two,
by = by_cols
)
}
-testing
clean_and_join(data_one = data_a, data_two = data_b, column_id_one = col_ltr,
column_id_two = col_ltr, col_nums_one = col_nums,
col_nums_two = col_nums)
# A tibble: 13 x 3
col_ltr col_nums.x col_nums.y
# <chr> <int> <dbl>
# 1 b 2 NA
# 2 d 4 NA
# 3 f 6 NA
# 4 h 8 NA
# 5 j 10 NA
# 6 l 12 NA
# 7 n 14 NA
# 8 p 16 NA
# 9 r 18 NA
#10 t 20 NA
#11 v 22 NA
#12 x 24 NA
#13 z 26 NA
Using by argument programmatically in join functions dplyr
The naming should be reversed in setNames
i.e. whatever is the name of the variable, it should match the first dataset by
variable
library(dplyr)
left_join(int_terms_df,terms_df[,c("terms","xs")],
by=setNames(xtemp, names_vars[1]))
-output
sex st xs
1 b f x_1
2 c f x_2
3 b g x_1
4 c g x_2
It is also TRUE for right_join
> right_join(int_terms_df,terms_df[,c("terms","xs")],
by=setNames(xtemp, names_vars[1]))
sex st xs
1 b f x_1
2 c f x_2
3 b g x_1
4 c g x_2
5 f <NA> x_3
6 g <NA> x_4
R - dplyr merge in user-defined function
This should work:
merge_tables <- function(inputdata1, inputdata2, byvar1, byvar2) {
byvar1 <- enquo(byvar1)
byvar2 <- enquo(byvar2)
by <- setNames(quo_name(byvar2), quo_name(byvar1))
outputdata <- full_join(inputdata1, inputdata2, by = by)
return(outputdata)
}
Returning:
testing_merge <- merge_tables(data1, data2, ID, PatID)
testing_merge
ID letter1 letter2
1 1 o r
2 2 x q
3 3 <NA> b
How do I pipe into or include an additional argument within a list of quosures?
One approach is to use quosure arithmetic:
purrr::map( my_q_list, ~quo( mtcars %>% !!.x ) ) %>% # Construct desired quosures
purrr::map( quo_squash ) %>% # Simplify them
purrr::map( eval_tidy ) # Evaluate them
Executing dplyr::left_join within a function using by=c(x=y) error
This has something to do with evaluation, i think (see here:https://adv-r.hadley.nz/evaluation.html) Maybe not...
I found a kind of hacky way, but it works for me:
test_function <- function(df1, df2, col1, col2){
helper <- col2
names(helper) <- col1
output_dataframe <- dplyr::left_join(df1, df2, by=helper)
return(output_dataframe)
}
concatenate quosures and string
Without a function, this is how you do it using dplyr
:
library(dplyr)
df %>%
select(starts_with("z_"))
You can also create a function and pass in a string for the variable name like this:
get_var= function(df, var){
df %>%
select(starts_with(paste0(var, "_")))
}
get_var(df, "z")
Now, the tricky part comes when you are trying to pass in the variable name without quoting it into a function (the R code, not the value it contains). One way of doing it would be deparse
+ substitute
in Base R. This converts the symbol supplied to var
to a string, which is convenient for later use within the function:
get_var = function(df, var){
var_quo = deparse(substitute(var))
df %>%
select(starts_with(paste0(var_quo, "_")))
}
Finally, here is how to do the same with enquo
and quo_name
in rlang/tidyverse
package:
library(rlang)
get_var = function(df, var){
var_quo = quo_name(enquo(var))
df %>%
select(starts_with(paste0(var_quo, "_")))
}
get_var(df, z)
get_var(df, y)
Result:
# A tibble: 1 x 2
z_1 z_2
<dbl> <dbl>
1 1 2
# A tibble: 1 × 2
y_1 y_2
<dbl> <dbl>
1 10 20
Notes:
- quosures are quoted expressions that keep track of an environment.
enquo
takes a symbol referring to a function's argument, quotes the R code and bundles it with the function's environment in a quosure.quo_name
formats a quosure into a string, which can be used later in the function.quo_text
is similar toquo_name
but does not check for whether the input is a symbol.
Check these:
rlang
documentation- Non-Standard Evaluation in R
?enquo
?quo_name
Related Topics
Identifying the Outliers in a Data Set in R
Rcpp Function to Select (And to Return) a Sub-Dataframe
Apply Function to Elements Over a List
All Possible Combinations of a Set That Sum to a Target Value
Convert a Printed Message into a Character Vector
Adding Percentage Labels on Pie Chart in R
Calculating Sum of Previous 3 Rows in R Data.Table (By Grid-Square)
Overlay Grid Rather Than Draw on Top of It
New R-Studio Version 0.98.932 Deletes .Md File - How to Prevent
Why Is := Allowed as an Infix Operator
Repeating Rows of Data.Frame in Dplyr
Using R to Read Out Excel-Colorinfo
Obtaining Connected Components of Neighboring Values
Sort a List of Nontrivial Elements in R
In R, Using Scientific Notation 10^ Rather Than E+
How to Write Contents of Help to a File from Within R
How to Add Shaded Confidence Intervals to Line Plot with Specified Values