R overflow
R overflow

Reputation: 1352

Recognize patterns in column, and add them to column in Data frame

Got a column with 50 keywords:

Keyword1 
Keyword2
Keyword3
KeywordN=50

In addition I got a data frame with two columns: Title and Abstract.

Title                    Abstract 
Rstudio Keyword1        A interesting program language keyword2  
Python Keyword3         A interesting program keyword3 language 

I want to get an extra column (let's call it Keywords), where the keyword name will appear IF it is in the Title or Abstract, like this:

Title             Abstract                                   Keywords
Rstudio Keyword1 A interesting program language keyword2  Keyword1, keyword2
Python Keyword2  A interesting program keyword3 language  Keyword2, Keyword3

The only thing how I could 'solve' this, was by making a binary columns (if a pattern matched). (grepl function), but that was not the desired solution...

Upvotes: 0

Views: 154

Answers (4)

moodymudskipper
moodymudskipper

Reputation: 47310

in base R:

  • This handles punctuation, spaces, end/starts of lines.
  • Keywords can contain spaces and some punctuation (but not all)
  • keywords in new column keep the case of original keyword vector:

code

ind  <- sapply(paste0('(^|[ [:punct:]])',tolower(keywords),'($|[ [:punct:]])'),grep,tolower(paste(df$Title,df$Abstract)))
ind[lengths(ind)==0] <- NA # for cases where no keyword is found
ind2 <- do.call(rbind,Map(data.frame,keyword=keywords,i=ind))
ind3 <- aggregate(keyword ~ i,ind2,paste,collapse=', ')
df$keywords[ind3$i] <- ind3$keyword
df$keywords[is.na(df$keywords)] <- "" # replacing NAs with empty strings
#              Title                                Abstract           keywords
# 1 Rstudio Keyword1 A interesting program language keyword2 Keyword1, Keyword2
# 2  Python Keyword2 A interesting program keyword3 language Keyword2, Keyword3

data

keywords <- c("Keyword1", "Keyword2", "Keyword3")

df <- read.table(text="Title                    Abstract 
                 'Rstudio Keyword1'        'A interesting program language keyword2'  
                 'Python Keyword2'         'A interesting program keyword3 language'",h=T,strin=F)

Upvotes: 1

DJack
DJack

Reputation: 4940

Another approach using strsplit (also in base R):

ls <- strsplit(tolower(paste(df$Title, df$Abstract)), 
                       "(\\s+)|(?!')(?=[[:punct:]])", perl = TRUE)    

df$Keywords <- do.call("rbind", 
               lapply(ls, function(x) paste(unique(x[x %in% tolower(keywords)]), 
               collapse = ", ")))

#             Title                                Abstract           Keywords
#1 Rstudio Keyword1 A interesting program language keyword2 keyword1, keyword2
#2  Python Keyword2 A interesting program keyword3 language keyword2, keyword3

Sample data

df <- data.frame(Title = c("Rstudio Keyword1", "Python Keyword2"), 
                 Abstract = c("A interesting program language keyword2",  
                              "A interesting program keyword3 language"), 
                 stringsAsFactors = F)

keywords <- paste0("Keyword", 1:4)

Upvotes: 1

Marta
Marta

Reputation: 27

Title<-as.character(c("Rstudio Keyword1","Python Keyword3"))
Abstract<-as.character(c("A interesting program language keyword2"," A interesting program keyword3 language"))
example1.data <- data.frame(Title,Abstract)


#loop answer
f<-length(example1.data)
example1.data$Keyword <- NA

for (i in 1:nrow(example1.data)){
testA[i]<-regmatches(example1.data$Title[i], regexpr("(Keyword|keyword) ([0-9])", example1.data$Title[i]))
testB[i]<-regmatches(example1.data$Abstract[i], regexpr("(Keyword|keyword)([0-9])", example1.data$Abstract[i]))
example1.data$Keyword[i]<-paste(testA[i],testB[i],  sep=", ")

}

Upvotes: 0

Onyambu
Onyambu

Reputation: 79208

cbind(dat,Keywords=do.call(paste,c(sep=",",Map(sub,paste0(".*(",paste(keywords,collapse="|"),").*"),"\\1",dat,TRUE))))
             Title                                Abstract          Keywords
1 Rstudio Keyword1 A interesting program language keyword2 Keyword1,keyword2
2  Python Keyword3 A interesting program keyword3 language Keyword3,keyword3

where keywords=paste0("Keyword",1:3) and

dat=read.table(text="Title                    Abstract 
'Rstudio Keyword1'        'A interesting program language keyword2'  
'Python Keyword3'         'A interesting program keyword3 language'",h=T,strin=F)

The line might seem long: Breakdown:

a=paste0(".*(",paste(keywords,collapse="|"),").*")
b=do.call(paste,c(sep=",",Map(sub,a,"\\1",dat,TRUE)))
cbind(dat,keywords=b)
             Title                                Abstract          keywords
1 Rstudio Keyword1 A interesting program language keyword2 Keyword1,keyword2
2  Python Keyword3 A interesting program keyword3 language Keyword3,keyword3

Upvotes: 1

Related Questions