[R] apply family functions
jim holtman
jholtman at gmail.com
Fri Aug 6 14:20:13 CEST 2010
The data you provided does not match your example:
> dat <- data.frame(Class = c("AAA", "C", "CC", "BB", "B", "A"), Close_date =
+ c(ini, ini, ini, ini+109, ini+39, ini+24), stringsAsFactors = FALSE)
> dat
Class Close_date
1 AAA 2010-01-01
2 C 2010-01-01
3 CC 2010-01-01
4 BB 2010-04-20
5 B 2010-02-09
6 A 2010-01-25
>
In your example output, why is line 2 invalid? Also in your 'for'
loop 'ind' was not defined.
On Fri, Aug 6, 2010 at 12:59 AM, Steven Kang <stochastickang at gmail.com> wrote:
> Hi all,
>
> I would like to flag each record in the data according to certain conditions
> as specified below.
>
> For example,
>
> If "Close_date" in *dat* is between ("Open" & "Close") or ("Open1" &
> "Close1") or ("Open2" & "Close2") in *oc, *flag the records as "Valid",
> otherwise "Invalid"
>
> I would like to use sapply family functions if possible due to the nature of
> large data set.
>
>
> ini <- as.Date("2010/1/1", "%Y/%m/%d")
> # Generate arbitrary data frame consisting of date values
> oc <- data.frame(Open = seq(ini, ini + 5, 1), Close = seq(ini + 365, ini +
> 365 + 5, 1), Open1 = seq(ini + 365*2, ini + 365*2 + 5, 1), Close1 = seq(ini
> + 365*3, ini + 365*3 + 5, 1), Open2 = seq(ini + 365*4, ini + 365*4 + 5, 1),
> Close2 = seq(ini + 365*5, ini + 365*5 + 5, 1))
> rownames(oc) <- c("AAA", "AA", "A", "BBB", "BB", "B")
> oc
> Open Close Open1 Close1
> Open2 Close2
> AAA 2010-01-01 2011-01-01 2012-01-01 2012-12-31 2013-12-31
> 2014-12-31
> AA 2010-01-02 2011-01-02 2012-01-02 2013-01-01 2014-01-01
> 2015-01-01
> A 2010-01-03 2011-01-03 2012-01-03 2013-01-02 2014-01-02
> 2015-01-02
> BBB 2010-01-04 2011-01-04 2012-01-04 2013-01-03 2014-01-03
> 2015-01-03
> BB 2010-01-05 2011-01-05 2012-01-05 2013-01-04 2014-01-04
> 2015-01-04
> B 2010-01-06 2011-01-06 2012-01-06 2013-01-05 2014-01-05
> 2015-01-05
>
>
> dat <- data.frame(Class = c("AAA", "C", "CC", "BB", "B", "A"), Close_date =
> c(ini, ini, ini, ini+109, ini+39, ini+24), stringsAsFactors = FALSE)
> dat
> Class Close_date
> 1 AAA 2010-01-01
> 2 A 2010-01-01
> 3 CC 2010-01-01
> 4 BB 2010-04-20
> 5 B 2010-02-09
> 6 A 2010-01-25
>
>
> Desired result is:
>
> Class Close_date Flag
> 1 AAA 2010-01-01 Valid
> 2 A 2010-01-01 Invalid
> 3 CC 2010-01-01 Valid
> 4 BB 2010-04-20 Valid
> 5 B 2010-02-09 Valid
> 6 A 2010-01-25 Valid
>
>
> The following seems to be a failure..
>
> for (i in length(ind)) {
> dat[["Flag"]] <- sapply(dat$Close_date, function(x) ifelse((x >=
> oc[ind[i], 1] & x < oc[ind[i], 2]) | (x >= oc[ind[i], 3] & x < oc[ind[i],
> 4]) | (x >= oc[ind[i], 5] & x < oc[ind[i], 6]) | (x >= oc[ind[i], 7] & x <
> oc[ind[i], 8]), "Valid", "Invalid"))
> }
>
> Any resolution in this would be greatly appreciated.
>
> Many thanks in advance.
>
>
>
> --
> Steven
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list