Skip to content

data format

1 message · arun

#
Hi,
Try this:
el<- read.csv("el.csv",header=TRUE,sep="\t",stringsAsFactors=FALSE)
?elsplit<- split(el,el$st)
?
datetrial<-data.frame(date1=seq.Date(as.Date("1930.1.1",format="%Y.%m.%d"),as.Date("2010.12.31",format="%Y.%m.%d"),by="day"))
elsplit1<- lapply(elsplit,function(x) data.frame(date1=as.Date(paste(x[,2],x[,3],x[,4],sep="-"),format="%Y-%m-%d"),discharge=x[,5]))
?elsplit2<-lapply(elsplit1,function(x) x[order(x[,1]),])
library(plyr)
elsplit3<-lapply(elsplit2,function(x) join(datetrial,x,by="date1",type="full"))
?elsplit4<-lapply(elsplit3,function(x) {x[,2][is.na(x[,2])]<- "-9999.000";x})
elsplit5<-lapply(elsplit4,function(x) {x[,1]<-format(x[,1],"%Y.%m.%d");x})
elsplit6<-lapply(elsplit5,function(x){substr(x[,1],6,6)<-ifelse(substr(x[,1],6,6)==0," ",substr(x[,1],6,6));substr(x[,1],9,9)<- ifelse(substr(x[,1],9,9)==0," ",substr(x[,1],9,9));x})
?elsplit6[[1]][1:4,]
#?????? date1 discharge
#1 1930. 1. 1 -9999.000
#2 1930. 1. 2 -9999.000
#3 1930. 1. 3 -9999.000
#4 1930. 1. 4 -9999.000

?length(elsplit6)
#[1] 124
?tail(elsplit6[[124]],25)
#?????????? date1 discharge
#29561 2010.12. 7 -9999.000
#29562 2010.12. 8 -9999.000
#29563 2010.12. 9 -9999.000
#29564 2010.12.10 -9999.000
#29565 2010.12.11 -9999.000
#29566 2010.12.12 -9999.000
#29567 2010.12.13 -9999.000
#29568 2010.12.14 -9999.000
#29569 2010.12.15 -9999.000
#29570 2010.12.16 -9999.000
#29571 2010.12.17 -9999.000
#29572 2010.12.18 -9999.000
#29573 2010.12.19 -9999.000
#29574 2010.12.20 -9999.000
#29575 2010.12.21 -9999.000
#29576 2010.12.22 -9999.000
#29577 2010.12.23 -9999.000
#29578 2010.12.24 -9999.000
#29579 2010.12.25 -9999.000
#29580 2010.12.26 -9999.000
#29581 2010.12.27 -9999.000
#29582 2010.12.28 -9999.000
#29583 2010.12.29 -9999.000
#29584 2010.12.30 -9999.000
#29585 2010.12.31 -9999.000

?str(head(elsplit6,3))
#List of 3
# $ AGOMO:'data.frame':??? 29585 obs. of? 2 variables:
?# ..$ date1??? : chr [1:29585] "1930. 1. 1" "1930. 1. 2" "1930. 1. 3" "1930. 1. 4" ...
? #..$ discharge: chr [1:29585] "-9999.000" "-9999.000" "-9999.000" "-9999.000" ...
?#$ AGONO:'data.frame':??? 29585 obs. of? 2 variables:
? #..$ date1??? : chr [1:29585] "1930. 1. 1" "1930. 1. 2" "1930. 1. 3" "1930. 1. 4" ...
? #..$ discharge: chr [1:29585] "-9999.000" "-9999.000" "-9999.000" "-9999.000" ...
?#$ ANZMA:'data.frame':??? 29585 obs. of? 2 variables:
? #..$ date1??? : chr [1:29585] "1930. 1. 1" "1930. 1. 2" "1930. 1. 3" "1930. 1. 4" ...
? #..$ discharge: chr [1:29585] "-9999.000" "-9999.000" "-9999.000" "-9999.000" ...


Regarding the space between date1 and discharge, I haven't checked it as you didn't mention whether it is needed in data.frame or not.

A.K.