简体   繁体   中英

Searching for row with string value in R data frame

i am new to R and i have question regarding searching for data.frame Row.

I have a column msg no tmp sensor lat lon alt 1 8d4008b858c381ff633cca3d1b59 0 277102796 13020203 0.00000 0.000000 0.00 2 8d4008b858c37575032db3f2f30e 1 136520046 13020203 51.03620 5.892563 11574.78 3 8d40690958af7480e6c539db2d28 2 902340359 13020203 0.00000 0.000000 0.00 4 8d4008b858c37574612e52e5843d 3 185870171 13020203 51.03243 5.904694 11574.78 5 8d4008b858c375764f2c6ea82b0e 4 615986062 13020203 51.04392 5.867767 11574.78 6 8d4008b858c375749f2e15a34831 5 665795000 13020203 51.03387 5.900040 11574.78 7 8d4008b858c37207a9349cd60077 6 576273468 13020203 51.04486 5.864621 11574.78 8 8d40690958af847ff0c66f60ea8e 7 742755281 13020203 0.00000 0.000000 0.00

the data frame is huge (1.5 million value). I need to check whether there is a row with particular msg . ie ,is there a row with msg=8d4008b858c37207a9349cd60077 (here row 7) . If so, return the no (here return 6) value. Also If there is no such value , it should be notified !

How can i do it efficiently for large data frame???

Thanks in advance

Try

library(data.table)#v1.9.5+
setDT(df1)[msg%chin% '8d4008b858c37207a9349cd60077', no]
#[1] 6

Or

setDT(df1, key='msg')[.('8d4008b858c37207a9349cd60077'), no]
#[1] 6

If we are checking for a value not in the 'msg' column, it will return NA

setDT(df1, key='msg')[.('xyz'), no]
#[1] NA

and to check for NA would be to use is.na

is.na( setDT(df1, key='msg')[.('xyz'), no])
#[1] TRUE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM