I am filling in the values of a column in a data frame from the values present in a matrix. The search for the value is performed from two columns called e1 and e2. But it is possible that in any of these columns contain strings that are not present in the matrix. This of course gives error, but I would like to know if it is possible to continue filling with a 0 that non-existent value.
x<-cbind(c(0.3,0.35,0.35,0),c(0.2,0.2,0.4,0.2)
,c(0,0.6,0.1,0.3),c(0.5,0.25,0.25,0))
colnames(x) <- c("A","B","C","D")
rownames(x) <- c("A","B","C","D")
y<-as.data.frame(cbind(c(1,2,3,4,5,6)
,c("A","A","B","A","B","A"),c("D","C","C","D","D","J")))
colnames(y) <- c("id","e1","e2")
index_df = y%>% select(e1,e2)
colnames(index_df)<-c('rows','cols')
y$l<-x[as.matrix(index_df)]
Error in x[as.matrix(index_df)] : subscript out of bounds