Dear all, I have a data frame that looks like this:> datV1 V2 V3 1 0.000 2 554889 2 2.001 0 9978 3 0.342 3 5263 4 123.000 0 3209 5 0.004 0 2434 I want to get a subset of that data frame where the entry in V1 has (x.000) as its decimal. yielding> wanted_datV1 V2 V3 1 0.000 2 554889 2 123.000 0 3209 What's the way to do it in R? - Gundala Viswanath Jakarta - Indonesia
On Apr 21, 2009, at 7:52 PM, Gundala Viswanath wrote:> Dear all, > > I have a data frame that looks like this: > >> dat > > V1 V2 V3 > 1 0.000 2 554889 > 2 2.001 0 9978 > 3 0.342 3 5263 > 4 123.000 0 3209 > 5 0.004 0 2434 > > > I want to get a subset of that data frame where > the entry in V1 has (x.000) as its decimal. > > yielding > >> wanted_dat > V1 V2 V3 > 1 0.000 2 554889 > 2 123.000 0 3209 > > What's the way to do it in R?At least two possibilities: # See if the truncated value == value # See ?trunc > subset(dat, trunc(V1) == V1) V1 V2 V3 1 0 2 554889 4 123 0 3209 # See if V1 mod 1 == 0 > subset(dat, V1 %% 1 == 0) V1 V2 V3 1 0 2 554889 4 123 0 3209 See ?subset as well. HTH, Marc Schwartz
On 21/04/2009 8:52 PM, Gundala Viswanath wrote:> Dear all, > > I have a data frame that looks like this: > >> dat > > V1 V2 V3 > 1 0.000 2 554889 > 2 2.001 0 9978 > 3 0.342 3 5263 > 4 123.000 0 3209 > 5 0.004 0 2434 > > > I want to get a subset of that data frame where > the entry in V1 has (x.000) as its decimal. > > yielding > >> wanted_dat > V1 V2 V3 > 1 0.000 2 554889 > 2 123.000 0 3209 > > What's the way to do it in R? > > - Gundala Viswanath > Jakarta - Indonesia >If you know that those values are really integers, you could use dat[ dat$V1 == round(dat$V1), ] but if you just want the printed version to have three zeroes after the decimal point, you'll want something with grep, e.g. dat[ grep("[.]000$", format(dat$V1)), ] (These are both untested.) Duncan Murdoch