Gonçalo Ferraz
2009-Jan-21 20:30 UTC
[R] finding row and column indices of date in multiple columns of a data frame
Hi, I have a data.frame SAMPLES with columns: Site Site# Season Day1 Day2 Day3 Day1, Day2, Day3 are class "Date", the other columns are numeric or factor. I have a date "mydate" that may or may not be listed in my data.frame and I need to find that out. If "mydate" is there, I want to get the number of the data.frame row where it occurs. "mydate" may be in any of the three date columns and if it is there, it appears only once. Thank you for any advice and sorry if this is ni FAQ's - I couldn't find it. Gonzalo
Jorge Ivan Velez
2009-Jan-21 20:41 UTC
[R] finding row and column indices of date in multiple columns of a data frame
Dear Gonçalo, If DF is your data set, something like this should do the job: which(DF==yourdate,arr.ind=TRUE)> # Example > Dates<-c('01/18/2009','01/19/2009','01/20/2009','01/21/2009') > DF<-data.frame(+ Site=sample(4), + "Site#"=sample(4), + Season=sample(4), + Day1=sample(Dates), + Day2= sample(Dates), + Day3=sample(Dates) + )> DFSite Site. Season Day1 Day2 Day3 1 2 3 1 01/21/2009 01/19/2009 01/19/2009 2 3 1 4 01/19/2009 01/18/2009 01/20/2009 3 1 2 2 01/20/2009 01/21/2009 01/18/2009 4 4 4 3 01/18/2009 01/20/2009 01/21/2009> which(DF=='01/21/2009',arr.ind=TRUE)row col [1,] 1 4 [2,] 3 5 [3,] 4 6 See ?which for more information. HTH, Jorge On Wed, Jan 21, 2009 at 3:30 PM, Gonçalo Ferraz <gferraz29@gmail.com> wrote:> Hi, > > I have a data.frame SAMPLES with columns: > > Site Site# Season Day1 Day2 Day3 > > Day1, Day2, Day3 are class "Date", the other columns are numeric or factor. > > I have a date "mydate" that may or may not be listed in my data.frame and I > need to find that out. > If "mydate" is there, I want to get the number of the data.frame row where > it occurs. > > "mydate" may be in any of the three date columns and if it is there, it > appears only once. > > Thank you for any advice and sorry if this is ni FAQ's - I couldn't find > it. > > Gonzalo > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]