Clearly I'm doing the if statement wrong but I don't see how it's
different from the subtraction so need my eyes opened a bit this
Sunday morning.
Thanks,
Mark
TestDF =
structure(list(
Trade = 1:10,
PosType = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
EnDate = c(1040106L, 1040107L, 1040107L, 1040108L,
1040108L, 1040108L, 1040109L, 1040112L, 1040112L, 1040113L)),
.Names = c("Trade","PosType", "EnDate"),
class = "data.frame",
row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
)
TestDF
TestDF$PrevDate <- with(TestDF, c(0, head(EnDate, -1)))
TestDF$NewDay <- with(TestDF, if (EnDate != PrevDate) 1 else 0)
TestDF$NewDay <- with(TestDF, EnDate - PrevDate)
TestDF
TestDF$NewDay <- with(TestDF, if (EnDate != PrevDate) 1 else 0)
OK, that works in terms of simply creating that column - which is
GOOD so THANKS - but somewhere down the road I'm going to need to do
an "if", or something equivalent. The requirement in the following
data frame is that on "new days" MargAvail = Initial-$4K, while on "!
new days" MargAvail = PrevMarg -$4K. If I use an if to try and get
there I just get the same problem.
I took a shot at trying to write values only when NewDay is TRUE or
FALSE. The code below writes Initial-$4K for all values and the I
tried to overwrite the previous-$4K when it's FALSE, but that is
creating a shorter list and R complains about that so it seems like
I'm back to the list issue.
I'm such a newb...
- Mark
DF =
structure(list(Trade = 1:10, PosType = c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), EnDate = c(1040106L, 1040107L, 1040107L,
1040108L, 1040108L, 1040108L, 1040109L, 1040112L, 1040112L, 1040113L
), EnTime = c(1227L, 641L, 915L, 909L, 930L, 953L, 1241L, 641L,
708L, 840L), ExDate = c(1040106L, 1040107L, 1040107L, 1040108L,
1040108L, 1040108L, 1040109L, 1040112L, 1040112L, 1040113L),
ExTime = c(1251L, 1306L, 1300L, 1300L, 1300L, 1301L, 1311L,
1306L, 1311L, 1311L), Pos_PL = c(-146L, 294L, 164L, 184L,
124L, 24L, -146L, 344L, 874L, 224L)), .Names = c("Trade",
"PosType", "EnDate", "EnTime", "ExDate", "ExTime", "Pos_PL"), class =
"data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10"))
DF
InitialCash = 10000
ReqMargin = 4000
DF$PLSum <- with(DF, cumsum(Pos_PL))
DF$Initial <- with(DF, InitialCash + c(0, head(cumsum(Pos_PL), -1)))
DF$PrevDate <- with(DF, c(0, head(EnDate, -1)))
DF$NewDay <- with(DF, EnDate != PrevDate)
### Fill column with Initial-$4K
DF$MargAvail <- with(DF, (Initial - ReqMargin) )
DF$PrevMarg <- with(DF, c(0, head(MargAvail, -1)))
### Overwrite with Previous-$4k - this fails becuase !NewDay isn't
valid all the time
DF$MargAvail <- with(DF, (PrevMarg - ReqMargin)[!NewDay] )
DF
I need to keep studying. This doesn't feel right.
- Mark
DF <- data.frame(cbind(a=1:4, b=1:2, c=1:8, d=1:16, e=0, f=0))
DF$Test <- with(DF, a == b)
DF$e = (DF$c*DF$d) * DF$Test + (DF$c+DF$d) * !DF$Test
DF$f = with(DF, (c*d)*Test + (c+d)*!Test)
DF
In general the ifelse option is the solution I chose yesterday
afternoon. My problem in reading the R help files was that it wasn't
clear (to me - stupid new R user) from those docs that there was any
difference between "if (A) B else C" vs ifelse(A,B,C) when actually
there is quite a difference.
I think the other problem has a lot to do with how much I burden you
guys with my data and descriptions of what I'm trying to do. My
inclination was to try and make the question very self contained in
some minimal example. In doing that maybe I create a data.frame in a
way some folks consider non-standard (but in my case does seem to have
value) but unfortunately causes folks to give answers about how to
create data.frames. That's OK - it's good info and helps me learn -
but it wasn't an answer to the root question.
I'm not sure how much information anyone here wants or how deeply to
go into what I'm trying to do. I'm happy to talk about it. There's
nothing greatly secret about this part of it, but it would take time
to write and maybe people aren't interested and all that ends up being
a waste of everyone's time. For me it's not about having a big
database and extracting information. It's more about having a little
bit of data, using it to develop a strategy to handle incoming data,
and then seeing how that strategy worked out using another portion of
the data, then repeat as necessary until I exhaust the data, etc.
- Mark