Word of notice: my experience with R is SO small.
I need to extract all columns from a dataframe, filtering by column "id" with value of 2
, but having column "id" in a variable, not hardcoded.
There's a couple of related questions, but I couldn't infer the solution out of them:
How to use a string variable to select a data frame column using $ notation
access data frame column using variable
My code:
df <- data.frame(id = c(1,2,3) , col_a=c("A1","A2","A3") , col_b=c("B1","B2","B3"))
pk_single_col <- "id"
#how do i translate this instruction NOT to use $id, but the variable pk_single_col?
spameggs <- df_a[df_a$id %in% 2 ,]
Thank you SO much in advance!