[R] elegant way to check if 2 values are in 3 columns?
David Winsemius
dwinsemius at comcast.net
Fri Aug 26 16:39:03 CEST 2011
On Aug 26, 2011, at 10:16 AM, Joanne Demmler wrote:
> Dear all,
>
> I'm trying to rerun some data linkage exercises in R (they are
> designed to be done in SPSS, SAS or STATA)
> The exercise in question is to relabel the column "treat" to "1", if
> "yearsep" is smaller than 1988 and columns "proc1"-"proc3" contain
> the values 56.36 or 59.81.
>
> My pathetic solution to do this in R currently looks like this:
>
> vaslinks4$treat <- 0
Why not just?
# skip the setting to 0 step
vaslinks4$treat <- as.integer( with( vaslinks,
yearsep < 1988 &
(proc2 %in% c(56.36,59.81) |
proc3 %in% c(56.36,59.81) )
) )
Maybe, but watch out about testing for equality or set membership when
using floating point numbers.
>
> vaslinks4$treat[vaslinks4$yearsep < 1988 && (vaslinks4$proc1 %in%
> c(56.36,59.81)
> || vaslinks4$proc2 %in% c(56.36,59.81)
> || vaslinks4$proc3 %in% c(56.36,59.81))] <- 1
A doomed strategy. Don't use "&&" or "||' when working with vectors.
>
> But I'm sure there is a more elegant solution for this, in which I
> would not have to call all three columns separately.
>
> Anyone?
> Yours Joanne
>
Snipped SAS and Stata code.
--
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list