I want to filter specific rows from my dataset and I want to define which row that is before the filter function, but whenever I do that I get 0 observations.
I want to do this (and this doesn't work since I get 0 observations):
name <- "my_dna_42_x"
gene <- "my_gene_12213"
df2 <- df1 %>% group_by(DNA, ID) %>% filter(any(DNA == name && ID == gene))
But this does work (but I don't want this since I want to be able to define name and gene before running it (and make it into a function later)):
df2 <- df1 %>% group_by(DNA, ID) %>% filter(any(DNA == "my_dna_42_x" && ID == "my_gene_12213"))
So how can I get the filter function to accept the name or ID while defining the names earlier?
(I also tried parse_expr(paste(name))
but that didn't work as well and I defined name as a symbol then like this: name <- sym("my_dna_42_x")
)
SOLVED: name was already a column name
QUESTION WITH EXAMPLE DATA:
set.seed(42)
n <- 6
dat <- data.frame(id=1:n,
date=seq.Date(as.Date("2020-12-26"), as.Date("2020-12-31"), "day"),
group=rep(LETTERS[1:2], n/2),
age=sample(18:30, n, replace=TRUE),
type=factor(paste("type", 1:n)),
x=rnorm(n))
my_type <- "type 1"
filtered_dat <- dat %>% group_by(id, type) %>% filter(type == "my_type")
I have an issue with defining my_type (last 2 lines) and calling it again.