I'm fairly new to R and XTS. Pardon the confusing title, the example below should illustrate my question better. The question, How to get value by column name in R?, didn't help me much, perhaps because I'm working with an XTS object.
I have a column of strings that are names of other columns in the XTS object.
xts_bars <- structure(c("1", "1", "1", "1", "-1", "-1", "-1", "-1", "action4",
"action4", "action", "action"), .indexCLASS = c("POSIXct", "POSIXt"),
tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts", "zoo"),
index = c(1506620100, 1506620400, 1506620700, 1506621000), .Dim = c(4L, 3L),
.Dimnames = list(NULL, c("action", "action4", "column_names")))
I want to create a new column and populate each row with the value from the named column at each row.
xts_bars$column_names
xts_bars$new_column = xts_bars[,xts_bars$column_names]
This is not working, it's creating 3 extra columns before the 'new_column' column:
action action4 column_names action4.1 action4.2 action.1 new_column
2017-09-28 12:35:00 "1" "-1" "action4" "-1" "-1" "1" "1"
2017-09-28 12:40:00 "1" "-1" "action4" "-1" "-1" "1" "1"
2017-09-28 12:45:00 "1" "-1" "action" "-1" "-1" "1" "1"
2017-09-28 12:50:00 "1" "-1" "action" "-1" "-1" "1" "1"
The 'new_column' column should contain -1, -1, 1, 1.