Skip to content

reshape

1 message · arun

#
Hi Elisa,

You need to check your data.? For some 'st', the data is repeated/duplicated (I am assuming, didn't check it) especially for a particular year.

dat1<-read.csv("elisa.csv",sep="\t")
dat1$st<- as.character(dat1$st)
?str(dat1)
#'data.frame':??? 506953 obs. of? 5 variables:
# $ st?????? : chr? "AGOMO" "AGOMO" "AGOMO" "AGOMO" ...
# $ year???? : int? 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 ...
# $ month??? : int? 1 1 1 1 1 1 1 1 1 1 ...
# $ day????? : int? 1 2 3 4 5 6 7 8 9 10 ...
# $ discharge: num? 6.75 7.6 5.58 5.02 4.8 ...

library(reshape2)
dat2<- do.call(rbind,lapply(split(dat1,list(dat1$st,dat1$year)),function(x) {x$day<-seq_along(x$day);x}))
row.names(dat2)<-1:nrow(dat2)
?str(dat2)
#'data.frame':??? 506953 obs. of? 5 variables:
# $ st?????? : chr? "TANNU" "TANNU" "TANNU" "TANNU" ...
# $ year???? : int? 1935 1935 1935 1935 1935 1935 1935 1935 1935 1935 ...
# $ month??? : int? 1 1 1 1 1 1 1 1 1 1 ...
# $ day????? : int? 1 2 3 4 5 6 7 8 9 10 ...
# $ discharge: num? 8.06 7.65 7.3 7.3 6.95 6.95 6.6 6.25 5.9 5.61 ...
res1<-lapply(split(dat2,dat2$st),function(x) dcast(x,day~year,mean,value.var="discharge"))
?length(res1)
#[1] 124
?which(lapply(res1,nrow)>366) #this is what I mentioned
#CURVO GHIST READO TERCA UZZCO 
#?? 34??? 52??? 82?? 115?? 118 
nrow(res1$CURVO)
#[1] 730

?nrow(res1$CURVO)
#[1] 730
?tail(res1$CURVO)
#??? day 2005 2006 2007 2008????? 2010
#725 725? NaN? NaN? NaN? NaN 1.2552028
#726 726? NaN? NaN? NaN? NaN 1.1714796
#727 727? NaN? NaN? NaN? NaN 0.8065988
#728 728? NaN? NaN? NaN? NaN 0.9375889
#729 729? NaN? NaN? NaN? NaN 1.1693797
#730 730? NaN? NaN? NaN? NaN 1.2124010


If the data is duplicated, then you can try this to sort out:
res1[lapply(res1,nrow)>366]<-lapply(res1[lapply(res1,nrow)>366],function(x) x[1:366,])
which(lapply(res1,nrow)>366)
#named integer(0)

tail(res1$TERCA)
#??? day???? 2004????? 2005????? 2006????? 2010
#361 361 4.451136 0.1457738 0.7695212 1.2963822
#362 362 4.539952 0.1371580 0.7409566 1.3431055
#363 363 2.133320 0.1179060 0.7325064 1.3905679
#364 364 1.499514 0.1238591 0.7223855 1.3318688
#365 365 1.136719 0.1312868 0.7449733 1.3561590
#366 366 1.004139?????? NaN?????? NaN 0.5610225


A.K.