R: reshape data, category to column name -


i have data set in r this:

bore_id     bore_date       result  obs_point_datum 2413437     29/06/1905 0:00 19.81   mahd 2413438     29/06/1905 0:00 18.9    mahd 2413439     29/06/1905 0:00 18.9    swl 2413441     29/06/1905 0:00 16.46   mahd 2413441     29/06/1905 0:00 12      swl 2413441     29/06/1905 0:00 12.5    dbns 60912270    29/06/1902 0:00 0       mahd .... 

i'm trying reshape group data bore_id , bore_date, looks this:

bore_id     bore_date       mahd  swl  dbns 2413441     29/06/1905 0:00 16.46 12   12.5 2413437     29/06/1905 0:00 19.81 na   na 2413438     29/06/1905 0:00 18.9  na   na    2413439     29/06/1905 0:00 na    18.9 na    ...  

i have tried using reshape2 package since reshape dosen't seem available newer versions of r.

here attempt:

#melt data based on id categories melt_data <- melt(data, id=c('bore_id','bore_date','obs_point_datum'))  #cast comparing id , date obs_point_datum cast_data <- dcast(data,'bore_id'+'bore_date'~'obs_point_datum', value ='result') 

this gives me following single line of data

"bore_id"   "bore_date" obs_point_datum  bore_id    bore_date   2.1 

any idea have gone wrong? jp

your data molten, don't need melt again.

library(reshape2) dcast(mydf, bore_id + bore_date ~ obs_point_datum, value.var = "result") #    bore_id       bore_date dbns  mahd  swl # 1  2413437 29/06/1905 0:00   na 19.81   na # 2  2413438 29/06/1905 0:00   na 18.90   na # 3  2413439 29/06/1905 0:00   na    na 18.9 # 4  2413441 29/06/1905 0:00 12.5 16.46 12.0 # 5 60912270 29/06/1902 0:00   na  0.00   na 

you can in base r with:

reshape(mydf, direction = "wide",          idvar = c("bore_id", "bore_date"),          timevar = "obs_point_datum") #    bore_id       bore_date result.mahd result.swl result.dbns # 1  2413437 29/06/1905 0:00       19.81         na          na # 2  2413438 29/06/1905 0:00       18.90         na          na # 3  2413439 29/06/1905 0:00          na       18.9          na # 4  2413441 29/06/1905 0:00       16.46       12.0        12.5 # 7 60912270 29/06/1902 0:00        0.00         na          na 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -