I want to set the value based on specific matching of rowname
and colname
in an R data frame. I have the row names (var1, var2, var3, var4 etc.) and the column names (x-var1-t1, x-var2-t1, x-var1-t4, x-var3-t1, x-var3-t7 etc). The row name needs to match the first "x-varN" part of the column name. For example, var1 row name should match with x-var1-t1 and x-var1-t4.
So this data frame:
x-var1-t1 x-var2-t1 x-var1-t4 x-var3-t1 x-var3-t7
var1 0 0 0 0 0
var2 0 0 0 0 0
var3 0 0 0 0 0
var4 0 0 0 0 0
would change to this:
x-var1-t1 x-var2-t1 x-var1-t4 x-var3-t1 x-var3-t7
var1 1 0 1 0 0
var2 0 1 0 0 0
var3 0 0 0 1 1
var4 0 0 0 0 0
What's the best way to perform this function?