I am trying extract values from a list using a for loop, and I am creating a function to pull those values by differing column names (x). The problem is the column names are nonstandard, e.g., they start with an integer. I know that we need to reference nonstandard column names with ``, but I'm having trouble understanding how I could use that in the following function:
But none of those work. I also tried putting {{colname}} in the function but that didn't work either. Anybody have an idea how I can achieve this (besides changing the column names, as this data comes from json files that are not named by me)?
when you have a level of indirection, and are metaprogramming, you should use [[]] syntax rather than $ syntax.
example :
(mylist <- list(
`a name` = "value"
))
# interactive useage i.e. programmer has typed it out
mylist$`a name`
# adding indirection , a variable contains the the thing to be used
var_to_use <- "a name"
# wont work; $ is for interactive uses and does not support evaluation
mylist$var_to_use
# works
mylist[[var_to_use]]