r - Accessing a matrix defined by paste (character string) -


i've created series matrices pasting county value based on loop word matrix. worked:

assign(paste("matrix",sort(unique(data$county), decreasing=false)[k],sep=""), matrix(0,100,100)) 

i want write different cells in matrix cannot. fails:

assign(paste("matrix",sort(unique(data$county), decreasing=false)[k],sep="")[j,i],1) 

the error in paste() since has "incorrect number of dimensions" since paste produces vector , [j,i] trying access matrix. i've tried wrap paste in get(), eval(), etc. different errors.

so question how make character string return matrix can access [j,i]?

you can use code scheme instead:

save list of counties, sorted want (decreasing=false default):

counties <- sort(unique(as.character(data$county))) 

create zeroed matrices per county:

matrices <- sapply(counties, function(.)matrix(0,100,100), simplify=false) 

write specific cells:

matrices[[counties[k]]][j,i] <- 1 

note: i've added as.character() avoid problems factors.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -