multidimensional array - R: creating a 3D matrix based on a colum value -



multidimensional array - R: creating a 3D matrix based on a colum value -

i have matrix has next first 2 columns: location, year. followed 50 columns, 1 each calendar day. columns days have either 'na' if location not visited, or 1 if location was.

example:

location year 1 2 3 4 5 6 7 8 9 10 .... 50 site1 2005 na na na 1 na na 1 na na 1 .... na site2 2006 na na 1 na na na 1 na na 1 .... na

i have used reshape bundle before create 4 dimensional matrices, each column in info frame used variable melt , cast array. here, have multiple columns , not seem work well.

i create following:

dim y: locations dim x: days 1-50 dim z: years

it's extracting info each year, , stacking each location day array 1 behind other. know best way create 3 dimensional array way might be?

thanks.

it not clear want have (your give values of final result not structure, , info work on)

first reproduce data

dat <- data.frame(location =c('site1','site2'), year=c(2005,2006)) dat <- cbind(dat,matrix(sample(c(1,na),100,rep=t),ncol=50))

then using reshape2 :

library(reshape2) melt(dat,id.vars=c('location','year')) location year variable value 1 site1 2005 1 na 2 site2 2006 1 na 3 site1 2005 2 1 4 site2 2006 2 na 5 site1 2005 3 na 6 site2 2006 3 1 7 site1 2005 4 1 8 site2 2006 4 na

r multidimensional-array

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -