reshape - Reshaping from wide to long in R -


i trying learn r , have question on reshaping following dataset.

bankname,date,year,month,quarter,totalliabilities,corr1,amt1,corr2,amt2 bank of pittsgurgh,2/7/1950,1950,2,1,237991,#n/a,#n/a,#n/a,#n/a bank of pittsgurgh,5/2/1950,1950,5,2,258865,#n/a,#n/a,#n/a,#n/a bank of pittsgurgh,8/7/1950,1950,8,3,218524,#n/a,#n/a,#n/a,#n/a,#n/a bank of pittsgurgh,11/6/1950,1950,11,4,237520,first bank,17472,third bank,30711 arsenal bank,2/2/1950,1950,2,1,218508,#n/a,#n/a,#n/a,#n/a arsenal bank,5/3/1950,1950,5,2,224110,#n/a,#n/a,#n/a,#n/a arsenal bank,8/2/1950,1950,8,3,216071,#n/a,#n/a,#n/a,#n/a arsenal bank,11/1/1950,1950,11,4,226166,national bank,20966,trust company,873 

when run following code reshape, following error. how can fix this? also, destring amt variable numeric variables , remove #na in dataset. how can destring variable?

-first tried create "id"

bank_test2$id<-as.numeric(as.factor(bank_test2$bankname)) 

-then tried create unique time variable using year , quarter

bank_test2$yq<-as.factor(paste(as.character(bank_test2$year),as.character(bank_test2$quarter)))    bank_test2<-bank_test2[with(bank_test2, order(yq,id)),]    

-reshape data

v <- outer(c("corr", "amt"), c(1:2), fun=paste0)    bank_test2<-reshape(bank_test2, direction='long', varying=c(v), sep='')         error in `row.names<-.data.frame`(`*tmp*`, value = paste(d[, idvar], times[1l],  :    duplicate 'row.names' not allowed in addition: warning message: non-unique values when setting 'row.names': ‘1.1’, ‘2.1’   id, bankname,   date,   year,   month,  quarter,    totalliabilities,   node,   corr,   amt       1,  bank of pittsgurgh, 2/7/1950,   1950,   2,  1,  237991, 1,  #n/a,   #n/a       1,  bank of pittsgurgh, 5/2/1950,   1950,   5,  2,  258865, 1,  #n/a,   #n/a    1,  bank of pittsgurgh, 8/7/1950,   1950,   8,  3,  218524, 1,  #n/a,   #n/a    1,  bank of pittsgurgh, 11/6/1950,  1950,   11, 4,  237520, 1,  first bank, 21906    1,  bank of pittsgurgh, 2/7/1950,   1950,   2,  1,  237991, 2,  #n/a,   #n/a    1,  bank of pittsgurgh, 5/2/1950,   1950,   5,  2,  258865, 2,  #n/a,   #n/a    1,  bank of pittsgurgh, 8/7/1950,   1950,   8,  3,  218524, 2,  #n/a,   #n/a    1,  bank of pittsgurgh, 11/6/1950,  1950,   11, 4,  237520, 2,  third bank, 4442    2,  arsenal bank,   2/2/1950,   1950,   2,  1,  218508, 1,  #n/a,   #n/a    2,  arsenal bank,   5/3/1950,   1950,   5,  2,  224110, 1,  #n/a,   #n/a    2,  arsenal bank,   8/2/1950,   1950,   8,  3,  216071, 1,  #n/a,   #n/a    2,  arsenal bank,   11/1/1950,  1950,   11, 4,  226166, 1,  national bank, 43224       2,  arsenal bank,   2/2/1950,   1950,   2,  1,  218508, 2,  #n/a,   #n/a    2   arsenal bank,   5/3/1950,   1950,   5,  2,  224110, 2,  #n/a,   #n/a    2   arsenal bank,   8/2/1950,   1950,   8,  3,  216071, 2,  #n/a,   #n/a    2   arsenal bank,   11/1/1950,  1950,   11, 4,  226166, 2,  trust company,  3682    

i want data organized way, newly created bankid "bankname" , create unique rownames using id , time value. want remove #na in dataset.
how should it?

thank in advance.

that particular error complaining rownames not being unique. avoid it, need pass reshape unique id each row "idvar". best approach create new column in original data frame such unique id, can use other field unique. example, totalliabilities unique in data frame, can use that:

bank_test2<-reshape(bank_test2, direction='long', varying=c(v), sep='',idvar="totalliabilities") 

that not best choice id hope points in right direction.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -