How to return a value for a variable (based on variable name) in the inline code?



I have a problem: I want to know how to, in the inline code, refer to what value a variable in a table has? The selection needs to be based on the variable name. See below example:

In the current text I want to write something like `r Town1$population` and then get the value (in this case 45) returned in the text. The important thing is that it is the variable name that causes me to get the number (in this case, Town1).

Town Population
Town1 45
Town2 43
Town3 67


In this case, the variable name is Town — “Town1” is one of the values in the Town variable. Both Town and Population are variables in your data frame (you didn’t say what the name of your data frame is, so I’m going to use dfm here). To retrieve the value of the Population variable where the Town variable equals “Town1”, you can write:

dfm$Population[dfm$Town == "Town1"]