I have a tibble that want to write a function to filter column x is character and use this function don't work.
tb <- tribble(
~x , ~y,
"a", 1,
"b", 2,
"c",3,
"d",4,
"a", 5,
"a", 3)
f <- function(name){ n <- tb %>% filter(x == name)}
I have a tibble that want to write a function to filter column x is character and use this function don't work.
tb <- tribble(
~x , ~y,
"a", 1,
"b", 2,
"c",3,
"d",4,
"a", 5,
"a", 3)
f <- function(name){ n <- tb %>% filter(x == name)}
What is not working exactly ?
Can you be more precise ? this seems to work fine, so I may have missed something...
library(dplyr)
#>
#> Attachement du package : 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
tb <- tribble(
~x , ~y,
"a", 1,
"b", 2,
"c",3,
"d",4,
"a", 5,
"a", 3)
f <- function(name){
tb %>% filter(x == name)
}
f("a")
#> # A tibble: 3 x 2
#> x y
#> <chr> <dbl>
#> 1 a 1
#> 2 a 5
#> 3 a 3
Created on 2020-03-28 by the reprex package (v0.3.0.9001)
thanks @cderv , but if use this function and assign to a variable, when i call function don't give me exist.
f <-function(name) {
z <- tb %>% filter(x == name)
return(z)
}
f("a")
t <- f("a")
it still works for me.
library(dplyr, warn.conflicts = FALSE)
tb <- tribble(
~x , ~y,
"a", 1,
"b", 2,
"c",3,
"d",4,
"a", 5,
"a", 3)
fun <-function(name) {
z <- tb %>% filter(x == name)
return(z)
}
fun("a")
#> # A tibble: 3 x 2
#> x y
#> <chr> <dbl>
#> 1 a 1
#> 2 a 5
#> 3 a 3
tab <- fun("a")
tab
#> # A tibble: 3 x 2
#> x y
#> <chr> <dbl>
#> 1 a 1
#> 2 a 5
#> 3 a 3
Created on 2020-03-29 by the reprex package (v0.3.0.9001)
Please try and build a reprex of your issue so that I can reproduce exactly what you encounter