Poumon
1
Hi everyone, I'm starting using R and I encounter a problem that I can't explain:
filter_table_and_extract_column <- function(data,column_to_filter_by_value,value, column_to_extract){
extracted_column <-(data %>% filter(column_to_filter_by_value == value))$column_to_extract
Return(extracted_column)
Error message:
`Preformatted text`Error in { (1-get_signature_infos.R#152): task 1 failed - "Problem with `filter()` input `..1`.
✖ object 'ONTOLOGY' not found
ℹ Input `..1` is `column_to_filter_by_value == value`."`Preformatted text`
In this case, the variable : column_to_filter_by_value is ONTOLOGY
Poumon
2
Ok I understand now that it's a non standard evaluation. So I realized that I can use quote(), unquote() and !!
I tried to write it like this but still doesn't do what I want:
filter_table_and_extract_column <- function(data,column_to_filter_by_value,value, column_to_extract){
column_to_filter_by_value <- enquo(column_to_filter_by_value)
column_to_extract <- as_label(enquo(column_to_extract))
extracted_column <- (data %>% filter(!!column_to_filter_by_value == value))[[column_to_extract]]
return(extracted_column)
}
library(dplyr)
filter_table_and_extract_column <- function(data, column_to_filter_by_value, value, column_to_extract) {
data %>%
filter({{column_to_filter_by_value}} == {{value}}) %>%
pull({{column_to_extract}})
}
filter_table_and_extract_column(iris, Species, "setosa", Sepal.Length)
#> [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1 5.7
#> [20] 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.0 5.5 4.9
#> [39] 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0
Created on 2021-02-13 by the reprex package (v1.0.0)
1 Like
Poumon
4
Thanks a lot, clear solution and it works perfectly !
system
Closed
6
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.