[R] VectorComparison

Joshua Wiley jwiley.psych at gmail.com
Mon Oct 18 15:00:19 CEST 2010


Hi,

You might consider ?match  For example:

StartDate = as.Date("01/10/2007", "%d/%m/%Y")
TimeSpan = seq(StartDate, by = 'days', length = 40)
TickLabels = as.Date(c("2007-10-01",  "2007-11-01",  "2007-12-01",
"2008-01-01"), "%Y-%m-%d")

TimeSpan[1:40] == TickLabels  # not designed for comparing multiple
values on each side
TickLabels %in% TimeSpan[1:40] # Find which TickLabels are in TimeSpane
TimeSpan[1:40] %in% TickLabels # Find which TimeSpane are in TickLabels


HTH,

Josh

On Mon, Oct 18, 2010 at 5:50 AM, Paolo Agnolucci
<agnolucponr at googlemail.com> wrote:
> Hi all,
>
> I am not exactly fluent in R and I got stuck with this. I would like to
> compare each elements of a vector A with any of the elements in Vector B.
> For some reasons it does not work.
>
>> StartDate = as.Date("01/10/2007", "%d/%m/%Y")
>> TimeSpan = seq(StartDate, by = 'days', length =
> length(myAverageCWVs$X1986))
>
>> TickLabels = c("2007-10-01",  "2007-11-01",  "2007-12-01",  "2008-01-01")
>
>> TimeSpan[1:40] == TickLabels
> Here I would expect TRUE for teh first entry and then TRUE for the entry
> related to "2007-11-01". This doesn't seem to be the case.
>
>  [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> [37] FALSE FALSE FALSE FALSE
>
>
> Thanks you in advance for any help.
>
>        [[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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list