Only fetch data from columns matching character strings
I have a dataset that looks something like this (but much more)
Jul_08 <- c(1,0,2,0,3)
Aug_08 <- c(0,0,1,0,1)
Sep_08 <- c(0,1,0,0,1)
month<-c("Jul_08","Aug_08","Jul_08","Sep_08","Jul_08")
dataset <- data.frame(Jul_08 = Jul_08, Aug_08 = Aug_08, Sep_08=Sep_08,month=month)
For each row, I would like to highlight the value for the selected month only, as specified in the "month" field. In other words, for a given row, if the "month" column is July_08, then for the new "value" column, I would like to include the anchor referring to the "Jul_08" column from that row.
In essence, the output will add this column of values ββto the dataset
value<-c(1,0,2,0,3)
Creating this final dataset
dataset.value<-cbind(dataset,value)
You can use matrix indexing:
w <- match(month, names(dataset))
dataset$value <- dataset[ cbind(seq_len(nrow(dataset)), w) ]
Here the vector w
specifies R where the column takes on from and is seq_len
used to use the same row, so the column value
is built by taking 1st column in 1st row, then 2nd column and 2nd row, 1st column for 3rd row, etc.
You can use lapply
:
value <- unlist(lapply(1:nrow(dataset),
function(r){
dataset[r,as.character(dataset[r,'month'])]
}))
> value
[1] 1 0 2 0 3
Or alternatively:
value <- diag(as.matrix(dataset[,as.character(dataset$month)]))
> value
[1] 1 0 2 0 3
Then you can cbind
create a new column as in your example.
Some notes:
- I prefer
unlist(lapply(...))
moresapply
, as the automatic simplification implemented in the sapply function sometimes surprises me. But I am sure that this time you can use it without any problem. -
as.character
only needed if the columnmonth
is a factor (as in the example), otherwise redundant (but I would leave it to be safe).