[R] finding the row(s) for a date in a data frame

Weiwei Shi helprhelp at gmail.com
Sat Aug 18 00:17:19 CEST 2007


try this, and you will get a clue:
> (today <- Sys.Date())
[1] "2007-08-17"
> class(today)
[1] "Date"
> as.Date("2007-08-17") == today
[1] TRUE

so basically,
your.target = as.Date("1979-12-14")

apply(A, c(1,2), function(x) x==your.target)) # should work. I did not
test though.

HTH,

Weiwei

On 8/17/07, Gonçalo Ferraz <gferraz29 at gmail.com> wrote:
> Hi,
>
> If I have a data frame A with the following format:
>
>        Day1          Day2           Day3          Day4
> 1     1979-11-02  1979-11-03  1979-11-04  <NA>
> 2     1979-12-06  <NA>          <NA>         <NA>
> 3     1979-12-13  1979-12-14  1979-12-15  1979-12-16
> 4     1979-12-20  <NA>          <NA>         <NA>
>
> And a date "1979-12-14", for example.
>
> How can I find the row or rows of the data frame were that date can be
> found?
>
> The date is stored in a date format variable called 'day'. I am trying
> commands such as A==day, expecting to see a matrix of logical values, but I
> am only getting error messages such as "(list) object cannot be coerced to
> 'double'".
>
> I am looking for a simple command that would return, in this case, a vector:
>
> FALSE FALSE TRUE FALSE
>
> Thanks for any help!
>
> Gonçalo
>
>         [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
>


-- 
Weiwei Shi, Ph.D
Research Scientist
GeneGO, Inc.

"Did you always know?"
"No, I did not. But I believed..."
---Matrix III



More information about the R-help mailing list