R: Find Matching Search Term in csv file -
i know how find exact lower case string under column search terms.
what did:
using r studio, read in csv file using command:
books = read.csv("~/desktop/r class /bbbooklist.csv", sep=",",header= true,na.strings = "?",stringsasfactors = false)
the csv file has header:
[1] "department" "search.term" "search.frequency" "asin" [5] "x.1.clicked.title" "click.share" "conversion.share" "asin.1" [9] "x.2.clicked.title" "click.share.1" "conversion.share.1" "asin.2" [13] "x.3.clicked.title" "click.share.2" "conversion.share.2"
here image of file called books:
what want retrieve rows search term: 'adult coloring books' , save rows new file called keywords.csv
i have tried several options like:
dta.subset<-subset(books,search.term = 'adult coloring books')
as grep commands like:
grep(pattern = "adult coloring books",x = string, value = t)
from op's question, isn't clear if term can appear in column or in 1 specific column. i'll assume can appear in column, since more generic. if question 1 specific column, solution may yield wanted results depending on data structure.
# dummy data frame set.seed(1) df = data.frame(matrix(sample(letters, size = 50, replace = t), nrow=10, ncol=5)) df x1 x2 x3 x4 x5 1 g f y m v 2 j e f p q 3 o r q m u 4 x j d e o 5 f u g v n 6 x m k r u 7 y s u 8 r z j c m 9 q j w s t 10 b u k s # want rows there's "a" in @ least 1 column # vector of logic indexes, telling row has letter "a" x = apply(df, 1, function(x) {any(grepl(x, pattern = "a")) }) # want df[x, ] x1 x2 x3 x4 x5 7 y s u
now, if don't need match regular expressions, can made simpler:
ind = apply(df=="a", 1, function(x) any(x)) df[ind, ]
edit:
now question edited realize op should study subsetting in r, suggested in comments.
df[df$x3=="a", ] x1 x2 x3 x4 x5 7 y s u
Comments
Post a Comment