I build up the column names you need as strings with paste. When you have column names stored in variables, you need to use [ rather than $, but otherwise they work just as well.
Sooner or later most R beginners are bitten by this all too convenient shortcut. As an R newbie,
think of R as your bank account: overuse of $-extraction can lead to undesirable consequences.
It's best to acquire the '[[' and '[' habit early.
-- Peter Ehlers (about the use of $-extraction)
R-help (March 2013)
The other (more generalizable) way to handle this problem would be to "melt" your data into long format - you would have a single treatment column with values A1, A2, ... and then single columns for m, nm, bd, crit. Multiple rows per id (one row per treatment per id). That would lend itself to a data.table or dplyr solution. Perhaps someone else will post an example.
Asked in February 2016Viewed 2,694 timesVoted 6Answered 1 times