So, I have a dataset (usage) that is like the following, in R:
Item Bike Usage
item1 bike1 1
item2 bike1 2
item1 bike2 1
item3 bike2 2
And I wanted to convert on a matrix with the usage. I created a matrix where my header is the bikes (bike 1, bike2), and the row names is (item 1, item 2, item 3), and filled with NA and now I want to fill it the usage, such as:
bike1 bike2
item 1 1 1
item 2 1 0
item 3 0 1
I created the following loop:
for (i in 1:nrow(usage)) {
item<-usage[i,1]
bike<-usage[i,2]
matriz[item,bike]<-usage[i,3]
matriz[is.na(matriz)]<-0
}
But I get this error:
Error in `[<-`(`*tmp*`, item, bike, value = list(Usage = 6)) :
invalid subscript type 'list'