Using grep to help subset a data frame in R

廉价感情. 提交于 2019-11-26 15:46:10

问题


I am having trouble subsetting my data. I want the data subsetted on column x, where the first 3 characters begin G45.

My data frame:

 x <- c("G448", "G459", "G479", "G406")  
 y <- c(1:4)
 My.Data <- data.frame (x,y)

I have tried:

 subset (My.Data, x=="G45*")

But I am unsure how to use wildcards. I have also tried grep() to find the indicies:

 grep  ("G45*", My.Data$x)

but it returns all 4 rows, rather than just those beginning G45, probably also as I am unsure how to use wildcards.


回答1:


It's pretty straightforward using [ to extract:

grep will give you the position in which it matched your search pattern (unless you use value = TRUE).

grep("^G45", My.Data$x)
# [1] 2

Since you're searching within the values of a single column, that actually corresponds to the row index. So, use that with [ (where you would use My.Data[rows, cols] to get specific rows and columns).

My.Data[grep("^G45", My.Data$x), ]
#      x y
# 2 G459 2

The help-page for subset shows how you can use grep and grepl with subset if you prefer using this function over [. Here's an example.

subset(My.Data, grepl("^G45", My.Data$x))
#      x y
# 2 G459 2

As of R 3.3, there's now also the startsWith function, which you can again use with subset (or with any of the other approaches above). According to the help page for the function, it's considerably faster than using substring or grepl.

subset(My.Data, startsWith(as.character(x), "G45"))
#      x y
# 2 G459 2



回答2:


You may also use the stringr package

library(dplyr)
library(stringr)
My.Data %>% filter(str_detect(x, '^G45'))

You may not use '^' (starts with) in this case, to obtain the results you need



来源:https://stackoverflow.com/questions/21311386/using-grep-to-help-subset-a-data-frame-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!