How to subset rows with only odd numbers in 1 column in R

7.9k views Asked by At

I want to compile only rows containing odd numbers in one of the columns. An example of my data frame is below:

V1       V2     V3       V4        V5     V6      V7       V8
14221    USDJPY 20030507 20:00:00  116.33 116.19  116.47   116.25
14222    USDJPY 20030507 21:00:00  116.24 116.24  116.42   116.32
14223    USDJPY 20030507 22:00:00  116.33 116.29  116.42   116.40
14224    USDJPY 20030507 23:00:00  116.42 116.34  116.44   116.42
14227    USDJPY 20030508 02:00:00  116.48 116.42  116.67   116.58
14228    USDJPY 20030508 03:00:00  116.57 116.50  116.65   116.62
14229    USDJPY 20030508 04:00:00  116.61 116.59  116.67   116.62
14230    USDJPY 20030508 05:00:00  116.63 116.62  116.72   116.63
14231    USDJPY 20030508 06:00:00  116.64 116.56  116.64   116.62
14232    USDJPY 20030508 07:00:00  116.62 116.57  116.67   116.58
14264    USDJPY 20030509 15:00:00  117.40 117.29  117.44   117.29
14265    USDJPY 20030509 16:00:00  117.30 117.19  117.34   117.20
14266    USDJPY 20030509 17:00:00  117.19 117.19  117.26   117.19
14267    USDJPY 20030509 18:00:00  117.18 117.17  117.26   117.20
14268    USDJPY 20030509 19:00:00  117.22 117.17  117.25   117.19
14269    USDJPY 20030509 20:00:00  117.18 117.17  117.24   117.22
14270    USDJPY 20030509 21:00:00  117.23 117.18  117.25   117.18
14271    USDJPY 20030512 00:00:00  117.25 117.08  117.25   117.09
14272    USDJPY 20030512 01:00:00  117.10 116.59  117.10   116.99
14273    USDJPY 20030512 02:00:00  116.98 116.89  117.01   116.93
14274    USDJPY 20030512 03:00:00  116.94 116.84  116.94   116.88

I would like to take a subset of data which contain rows that has odd numbers in column V3 such that my new data frame will be as below:

V1       V2     V3       V4        V5     V6      V7       V8
14221    USDJPY 20030507 20:00:00  116.33 116.19  116.47   116.25
14222    USDJPY 20030507 21:00:00  116.24 116.24  116.42   116.32
14223    USDJPY 20030507 22:00:00  116.33 116.29  116.42   116.40
14224    USDJPY 20030507 23:00:00  116.42 116.34  116.44   116.42
14264    USDJPY 20030509 15:00:00  117.40 117.29  117.44   117.29
14265    USDJPY 20030509 16:00:00  117.30 117.19  117.34   117.20
14266    USDJPY 20030509 17:00:00  117.19 117.19  117.26   117.19
14267    USDJPY 20030509 18:00:00  117.18 117.17  117.26   117.20
14268    USDJPY 20030509 19:00:00  117.22 117.17  117.25   117.19
14269    USDJPY 20030509 20:00:00  117.18 117.17  117.24   117.22
14270    USDJPY 20030509 21:00:00  117.23 117.18  117.25   117.18
4

There are 4 answers

7
Prolix On BEST ANSWER

The question is quite badly formulated but if I understand it correctly it would mean doing something like that:

col=3 #col is the # of the column where the numbers should be odd
dat[dat[,col]%%2==1,] #dat is the data frame or matrix containing your data

Or in the example you give you can also do

dat<-read.table("clipboard",header=T) #after having CTR+C the example frame from the question
dat[dat$V3 %% 2 == 1,]
3
rmuc8 On

For a particular column

df[df$V3 %% 2 != 0, ]

if your data.frame is df

Assuming you want to subset rows with an odd number in any column

If your data is

a <- c(1,2,3,4,5,6)
b <- c(1,2,3,5,6,6)
df <- data.frame(a,b)

> df
  a b
1 1 1
2 2 2
3 3 3
4 4 5
5 5 6
6 6 6 

try

df <- df[rowSums(df %% 2)!=0, ]

> df
  a b
1 1 1
3 3 3
4 4 5
5 5 6
0
Pierre L On

For ease, create a function that tests for odd numbers. It's a two-liner, but everything great doesn't happen in one line. I +1 SabDem's dplyr answer. I like that one.

is.odd <- function(v) v %% 2 != 0

And you can use that function for any other instance having to do with odd numbers.

df1[is.odd(df1$V3), ]
      V1     V2       V3       V4     V5     V6     V7
1  14221 USDJPY 20030507 20:00:00 116.33 116.19 116.47
2  14222 USDJPY 20030507 21:00:00 116.24 116.24 116.42
3  14223 USDJPY 20030507 22:00:00 116.33 116.29 116.42
4  14224 USDJPY 20030507 23:00:00 116.42 116.34 116.44
11 14264 USDJPY 20030509 15:00:00 117.40 117.29 117.44
12 14265 USDJPY 20030509 16:00:00 117.30 117.19 117.34
       V8
1  116.25
2  116.32
3  116.40
4  116.42
11 117.29
12 117.20
0
SabDeM On

Here is my solution with dplyr:

library(dplyr)
data %>% filter(V3 %% 2 == 1)