Hi Arun, thank-you very much! The 2nd option worked perfectly. That was
what I wanted.
Now, I have another question. I am using the R packages dataRetrieval
and EGRET from https://github.com/USGS-CIDA/WRTDS.
I have 2 objects Daily and Sample that have the naming convention (Names
= "21NC02WQ.C1000000" or whatevver the list of site names happens to
be)
that I need to have after running modelEstimation, but the names are
stripped after running modelEstimation (see below). The new names after
running modelEstimation for both Sample and Daily are the names of the
columns rather than the site name.
## modelEstimation -- this is the code that I use to do the loop through
modelEstimation
for(i in Daily) {
modelEstimation(localDaily = Daily[[i]], localSample = Sample[[i]],
localINFO = INFO[[i]], windowY=10, windowQ=2,
windowS=0.5,minNumObs=10,minNumUncen=6, env=parent.frame())
}
# shows the correct naming convention before being ran through
modelEstimation> dput(Daily)
structure(list(`21NC02WQ.C1000000` = structure(list(Date structure(c(11231,
11232, 11233, 11234, 11235, 11236), class = "Date"), Q
c(0.886534950681092,
0.857937049046218, 0.829339147411344, 0.80074124577647,
0.772143344141596,
0.743545442506722), Julian = c(55060, 55061, 55062, 55063, 55064,
55065), Month = c(10, 10, 10, 10, 10, 10), Day = c(275, 276,
277, 278, 279, 280), DecYear = c(2000.75, 2000.75273224044,
2000.75546448087,
2000.75819672131, 2000.76092896175, 2000.76366120219), MonthSeq c(1810,
1810, 1810, 1810, 1810, 1810), i = 1:6, LogQ = c(-0.120434728772853,
-0.153224551595844, -0.187126103271526, -0.222217423082796,
-0.258585067253671,
-0.296325395236517)), .Names = c("Date", "Q",
"Julian", "Month",
"Day", "DecYear", "MonthSeq", "i",
"LogQ"), row.names = c(NA,
-6L), class = "data.frame")), .Names = "21NC02WQ.C1000000")
# shows the correct naming convention before being ran through
modelEstimation> dput(Sample)
structure(list(`21NC02WQ.C1000000` = structure(list(Date structure(numeric(0),
class = "Date"),
ConcLow = numeric(0), ConcHigh = numeric(0), Uncen = numeric(0),
ConcAve = numeric(0), Julian = numeric(0), Month = numeric(0),
Day = numeric(0), DecYear = numeric(0), MonthSeq = numeric(0),
SinDY = numeric(0), CosDY = numeric(0)), .Names = c("Date",
"ConcLow", "ConcHigh", "Uncen",
"ConcAve", "Julian", "Month",
"Day", "DecYear", "MonthSeq", "SinDY",
"CosDY"), row.names = integer(0),
class = "data.frame")), .Names = "21NC02WQ.C1000000")
# shows the Daily incorrect naming convention after being ran through
modelEstimation
)))), .Names = c("Date", "Q", "Julian",
"Month", "Day", "DecYear",
"MonthSeq", "i", "LogQ", "Q7",
"Q30", "yHat", "SE", "ConcDay",
"FluxDay", "FNConc", "FNFlux"), row.names = c(NA,
-1461L), class "data.frame")
# shows the Sample incorrect naming convention after being ran through
modelEstimation
, .Names = c("Date", "ConcLow",
"ConcHigh", "Uncen", "ConcAve",
"Julian", "Month", "Day", "DecYear",
"MonthSeq", "SinDY", "CosDY", "Q",
"LogQ", "yHat", "SE", "ConcHat"
), row.names = 176:198, class = "data.frame")
Is there anyway to make the Name of the object Daily and Sample the site
name after being processed through modelEstimation, i.e. making the
current names the column names which they should be?
I also have a question about the INFO object that I will address in the
next e-mail.
Thank you for all of your continued great assistance Arun!
Irucka
## the modelEstimation function and its major components are below
modelEstimation
function (localDaily = Daily, localSample = Sample, localINFO = INFO,
windowY = 10, windowQ = 2, windowS = 0.5, minNumObs = 100,
minNumUncen = 50, env = parent.frame())
{
cat("\n first step running estCrossVal may take about 1 minute")
Sample1 <- estCrossVal(SampleCrossV = localSample, windowY,
windowQ, windowS, minNumObs, minNumUncen)
surfaceIndexParameters <- surfaceIndex(localDaily = localDaily)
localINFO$bottomLogQ <- surfaceIndexParameters[1]
localINFO$stepLogQ <- surfaceIndexParameters[2]
localINFO$nVectorLogQ <- surfaceIndexParameters[3]
localINFO$bottomYear <- surfaceIndexParameters[4]
localINFO$stepYear <- surfaceIndexParameters[5]
localINFO$nVectorYear <- surfaceIndexParameters[6]
localINFO$windowY <- windowY
localINFO$windowQ <- windowQ
localINFO$windowS <- windowS
localINFO$minNumObs <- minNumObs
localINFO$minNumUncen <- minNumUncen
cat("\nNext step running estSurfaces with survival regression:\n")
surfaces1 <- estSurfaces(localDaily = localDaily, localSample localSample,
windowY, windowQ, windowS, minNumObs, minNumUncen)
Daily1 <- estDailyFromSurfaces(localDaily = localDaily, localINFO localINFO,
localsurfaces = surfaces1)
env$Daily <- Daily1
env$INFO <- localINFO
env$Sample <- Sample1
env$surfaces <- surfaces1
cat("\nDone with modelEstimation,\nnow do
AnnualResults<-setupYears()\nor if using a period of analysis other than
Water Year specify the arguments paStart and paLong in call to
setupYears ")
}
<environment: namespace:EGRET>
> estSurfaces
function (localDaily = Daily, localSample = Sample, windowY = 10,
windowQ = 2, windowS = 0.5, minNumObs = 100, minNumUncen = 50)
{
bottomLogQ <- min(localDaily$LogQ) - 0.05
topLogQ <- max(localDaily$LogQ) + 0.05
stepLogQ <- (topLogQ - bottomLogQ)/13
vectorLogQ <- seq(bottomLogQ, topLogQ, stepLogQ)
stepYear <- 1/16
bottomYear <- floor(min(localDaily$DecYear))
topYear <- ceiling(max(localDaily$DecYear))
vectorYear <- seq(bottomYear, topYear, stepYear)
nVectorYear <- length(vectorYear)
estPtLogQ <- rep(vectorLogQ, nVectorYear)
estPtYear <- rep(vectorYear, each = 14)
resultSurvReg <- runSurvReg(estPtYear, estPtLogQ, localSample,
windowY, windowQ, windowS, minNumObs, minNumUncen)
surfaces <- array(0, dim = c(14, nVectorYear, 3))
for (iQ in 1:14) {
for (iY in 1:nVectorYear) {
k <- (iY - 1) * 14 + iQ
surfaces[iQ, iY, ] <- resultSurvReg[k, ]
}
}
return(surfaces)
}
<environment: namespace:EGRET>
> estDailyFromSurfaces
function (localDaily = Daily, localINFO = INFO, localsurfaces surfaces)
{
library("fields")
LogQ <- seq(localINFO$bottomLogQ, by = localINFO$stepLogQ,
length.out = localINFO$nVectorLogQ)
Year <- seq(localINFO$bottomYear, by = localINFO$stepYear,
length.out = localINFO$nVectorYear)
localDaily$yHat <- interp.surface(obj = list(x = LogQ, y = Year,
z = localsurfaces[, , 1]), loc = data.frame(localDaily$LogQ,
localDaily$DecYear))
localDaily$SE <- interp.surface(obj = list(x = LogQ, y = Year,
z = localsurfaces[, , 2]), loc = data.frame(localDaily$LogQ,
localDaily$DecYear))
localDaily$ConcDay <- interp.surface(obj = list(x = LogQ,
y = Year, z = localsurfaces[, , 3]), loc = data.frame(localDaily$LogQ,
localDaily$DecYear))
localDaily$FluxDay <- localDaily$ConcDay * localDaily$Q *
86.4
allLogQsByDayOfYear <- split(localDaily$LogQ, localDaily$Day)
allLogQsByDayOfYear[["365"]] <-
c(unlist(allLogQsByDayOfYear["365"]),
unlist(allLogQsByDayOfYear["366"]))
allLogQsByDayOfYear["366"] <- allLogQsByDayOfYear["365"]
allLogQsReplicated <- allLogQsByDayOfYear[localDaily$Day]
allDatesReplicated <- rep(localDaily$DecYear, lapply(allLogQsReplicated,
length))
allConcReplicated <- interp.surface(obj = list(x = LogQ,
y = Year, z = localsurfaces[, , 3]), loc = data.frame(unlist(x
allLogQsReplicated),
y = allDatesReplicated))
allFluxReplicated <- allConcReplicated * exp(unlist(allLogQsReplicated))
*
86.4
localDaily$FNConc <- tapply(allConcReplicated, allDatesReplicated,
"mean")
localDaily$FNFlux <- tapply(allFluxReplicated, allDatesReplicated,
"mean")
return(localDaily)
}
<environment: namespace:EGRET>
<-----Original Message-----> >From: arun kirshna [via R] [ml-node+s789695n4661331h37@n4.nabble.com]
>Sent: 3/14/2013 7:30:54 AM
>To: iruckaE@mail2world.com
>Subject: Re: "[[i]]$" <- "" indexing and lapply
>
>Hi Irucka,
>
>Not sure how `Q` should be attached to Data object.
>Try this:
>const<- list(1.01, 1.00, 0.99)
>DailyModeled1<-mapply(cbind,DailyModeled,const=const,SIMPLIFY=FALSE)
>
>DailyModeled$Q<-lapply(seq_along(Daily),function(i)
Daily[[i]]$Q*DailyModeled1[[i]]$const) > str(DailyModeled) #different than what you showed. Also, I noticed
that 'Q' is the same in your 'str' >#List of 4
># $ :'data.frame': 4 obs. of 1 variable:
># ..$ Date: Date[1:4], format: "2000-10-01" "2000-10-02"
...
># $ :'data.frame': 4 obs. of 1 variable:
># ..$ Date: Date[1:4], format: "2000-10-01" "2000-10-02"
...
># $ :'data.frame': 4 obs. of 1 variable:
># ..$ Date: Date[1:4], format: "2000-10-01" "2000-10-02"
...
># $ Q:List of 3
># ..$ : num [1:4] 1.316 1.03 0.944 0.858
># ..$ : num [1:4] 0.397 0.369 0.312 0.281
># ..$ : num [1:4] 76.3 78.8 78.5 75.7
>
>#Or
>#Maybe you want something like this:
>DailyModeled[]<-lapply(seq_along(DailyModeled1),function(i)
{DailyModeled[[i]]$Q<->Daily[[i]]$Q*DailyModeled1[[i]]$const;DailyModeled[[i]]} )
> DailyModeled
>#[[1]]
># Date Q
>#1 2000-10-01 1.3156007
>#2 2000-10-02 1.0296005
>#3 2000-10-03 0.9438005
>#4 2000-10-04 0.8580004
>
>#[[2]]
># Date Q
>#1 2000-10-01 0.3969416
>#2 2000-10-02 0.3686247
>#3 2000-10-03 0.3119910
>#4 2000-10-04 0.2808425
>
>#[[3]]
># Date Q
>#1 2000-10-01 76.25160
>#2 2000-10-02 78.77463
>#3 2000-10-03 78.49430
>#4 2000-10-04 75.69093
>
> str(DailyModeled)
>#List of 3
># $ :'data.frame': 4 obs. of 2 variables:
># ..$ Date: Date[1:4], format: "2000-10-01" "2000-10-02"
...
># ..$ Q : num [1:4] 1.316 1.03 0.944 0.858
># $ :'data.frame': 4 obs. of 2 variables:
># ..$ Date: Date[1:4], format: "2000-10-01" "2000-10-02"
...
># ..$ Q : num [1:4] 0.397 0.369 0.312 0.281
># $ :'data.frame': 4 obs. of 2 variables:
># ..$ Date: Date[1:4], format: "2000-10-01" "2000-10-02"
...
># ..$ Q : num [1:4] 76.3 78.8 78.5 75.7
>A.K.
>
>
>
>
>
>If you reply to this email, your message will be added to the
discussion below:>http://r.789695.n4.nabble.com/i-indexing-and-lapply-tp4661286p4661331.h
tml >To unsubscribe from "[[i]]$" <- "" indexing and
lapply, click here.
>NAML
<span id=m2wTl><p><font face="Arial, Helvetica,
sans-serif" size="2"
style="font-size:13.5px">_______________________________________________________________<BR>Get
the Free email that has everyone talking at <a href=http://www.mail2world.com
target=new>http://www.mail2world.com</a><br> <font
color=#999999>Unlimited Email Storage – POP3 – Calendar
– SMS – Translator – Much
More!</font></font></span>
--
View this message in context:
http://r.789695.n4.nabble.com/i-indexing-and-lapply-tp4661286p4662103.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]