2

Is there a way to get a filtered data frame, like this:

data[data$Measure=="Baseline",]

using a variable Name for Measure, i.e. measVarName == "Measure"?

Thanks.

2
  • 1
    Yeah, you can't use variables with $ but you can with [ and [[. So data[data[[measVarName]] == "Baseline", ] Commented Apr 24, 2018 at 14:33
  • Look at this example: measVarName <- "mpg"; mtcars[mtcars[measVarName]==19.2,] Commented Apr 24, 2018 at 14:35

1 Answer 1

3

Double bracket notation lets you select variables using a character string stored in a variable:

measVarName <- 'Measure'
data[data[[measVarName]] == 'Baseline',]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.