Site icon R-bloggers

Truncate by Delimiter in R

[This article was first published on Mollie's Research Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Sometimes, you only need to analyze part of the data stored as a vector. In this example, there is a list of patents. Each patent has been assigned to one or more patent classes. Let’s say that we want to analyze the dataset based on only the first patent class listed for each patent.
< !-- HTML generated using hilite.me -->
patents <- data.frame(
  patent = 1:30,
  class = c("405", "33/209", "549/514", "110", "540", "43", 
  "315/327", "540", "536/514", "523/522", "315", 
  "138/248/285", "24", "365", "73/116/137", "73/200", 
  "252/508", "96/261", "327/318", "426/424/512", 
  "75/423", "430", "416", "536/423/530", "381/181", "4", 
  "340/187", "423/75", "360/392/G9B", "524/106/423"))

We can use regular expressions to truncate each element of the vector just before the first “/”.

grep, grepl, sub, gsub, regexpr, gregexpr, and regexec are all functions in the base package that allow you to use regular expressions within each element of a character vector. sub and gsub allow you to replace within each element of the vector. sub replaces the first match within each element, while gsub replaces all matches within each element. In this case, we want to remove everything from the first “/” on, and we want to replace it with nothing. Here’s how we can use sub to do that:
< !-- HTML generated using hilite.me -->
patents$primaryClass <- sub("/.*", "", patents$class)

> table(patents$primaryClass)

110 138  24 252 315 327  33 340 360 365 381   4 405 416 423 426  43 430 523 524 
  1   1   1   1   2   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
536 540 549  73  75  96 
  2   2   1   2   1   1 



This post is one part of my series on Text to Columns.

Citations and Further Reading

To leave a comment for the author, please follow the link and comment on their blog: Mollie's Research Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.