I am trying to calculate a simple ratio using data.table. Different files have different tmax values, so that is why I need ifelse
. When I debug this, the dt looks good. The tmaxValue
is a single value (the first "t=60" encountered in this case), but t0Value
is all of the "t=0" values in dt.
summaryDT <- calculate_Ratio(reviewDT[,list(Result, Time), by=key(reviewDT)])
calculate_Ratio <- function(dt){
tmaxValue <- ifelse(grepl("hhep", inFile, ignore.case = TRUE),
dt[which(dt[,Time] == "t=240min"),Result],
ifelse(grepl("hlm",inFile, ignore.case = TRUE),
dt[which(dt[,Time] == "t=60"),Result],
dt[which(dt[,Time] == "t=30"),Result]))
t0Value <- dt[which(dt[,Time] == "t=0"),Result]
return(dt[,Ratio:=tmaxValue/t0Value])
}
What I am getting out is theResult
for tmaxValue
divided by all of the Result
's for all of the t0Value
's, but what I want is a single ratio for开发者_如何学C each unique by
.
Thanks for the help.
You didn't provide a reproducible example, but typically using ifelse
is the wrong thing to do.
Try using if(...) ... else ...
instead.
ifelse(test, yes, no)
acts very weird: It produces a result with the attributes and length from test
and the values from yes
or no
.
...so in your case you should get something without attributes and of length one - and that's probably not what you wanted, right?
[UPDATE] ...Hmm or maybe it is since you say that tmaxValue
is a single value...
Then the problem isn't in calculating tmaxValue
? Note that ifelse
is still the wrong tool for the job...
精彩评论