1

I got a data frame which containg a column containing codes separated by space. And I want to search those ids by applying "<" or ">" etc arithmatic operaters.

Input data frame:

df <- data.frame(Id=c(101, 102,103), Codes=c("1 2 3", "2 4 5", "4 5"))

I tried finding id's who have codes greater than 3. so that I can get 102 and 103 as output.

df[df$Codes > "3", ]

but this is giving me 103 id. what I am missing??

indra_patil
  • 283
  • 1
  • 4
  • 11
  • 2
    for now, you're comparing strings and, alphabetically, the strings "4 5" is the only one after the string "3". – Cath Apr 15 '16 at 06:43

1 Answers1

2

We can try

df[sapply(strsplit(as.character(df$Codes), "\\s+"), function(x) any(as.numeric(x)>3)), ]
#   Id Codes
#2 102 2 4 5
#3 103   4 5

Or use grep

df[grep("[4-9]", df$Codes),] 
#   Id Codes
#2 102 2 4 5
#3 103   4 5

If we need only the "Id"

df$Id[grep("[4-9]", df$Codes)] 
#[1] 102 103
akrun
  • 874,273
  • 37
  • 540
  • 662
  • This is really great. Thanks @akrun. But I have one query, cant we do it in a simple way just same as %like% operater does: df$Codes %like% '3' same way df$Codes <'3' – indra_patil Apr 18 '16 at 10:02
  • @indra_patil It is a string. So, the one simple way is using `grep` – akrun Apr 18 '16 at 10:09