4

Here is an example data set:

data <- data.frame (author = c('bob', 'john', 'james'), 
                    year = c(2000, 1942, 1765), 
                    title = c('test title one two three', 
                              'another test title four five', 
                              'third example title'))

And I would like to automate the process of making bibtex references, e.g. with a function like this:

bibtexify <- function (author, year, title) {
      acronym <- convert.to.acronym(title)
      paste(author, year, acronym, sep='')
      }

so that I get the following result:

with(data, bibtexify(author, year, title))
[1] 'bob2000tto'
[2] 'john1942att'
[3] 'james1765tet'

Is it possible to do this in R?

smci
  • 32,567
  • 20
  • 113
  • 146
David LeBauer
  • 31,011
  • 31
  • 115
  • 189
  • There are [251 Q&A on *\[r\] bibtex*](https://stackoverflow.com/search?q=%5Br%5D+bibtex+), this could be a duplicate. – smci Dec 10 '20 at 03:19

4 Answers4

10

you want abbreviate

R> abbreviate('test title one two three')
test title one two three 
             "ttott" 
Andrew Redd
  • 4,632
  • 8
  • 40
  • 64
4

Here is one possibility you could build from:

title <- c('test title one two three',  
                              'another test title four five',  
                              'third example title')
library(gsubfn)
sapply( strapply(title, "([a-zA-Z])[a-zA-Z]*"), function(x) paste(x[1:3], collapse=''))

This assumes that there are at least 3 words in each title, will need to be fixed if that is not the case.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
1

If you really want an acronym, and not an abbreviation you can use this functions:

acronymatize <- function(x) {
  s <- strsplit(x, " ")[[1]]
  paste((substring(s, 1,1)),
        sep="", collapse="")
}

Example:

> acronymatize("One Two Three")
[1] "OTT"
Patrick Williams
  • 694
  • 8
  • 22
0
accronym <- function(x)
{
  s <- strsplit(as.character(x)," ")
  s1 <- lapply(s,substring,1,1)
  s2 <- lapply(s1,paste,collapse="",sep="")
  unlist(s2)
}

The answer by Patrick works when there is a single element to be made an acronym. However, if there are more than one elements to be entered then probably the above function may work better. Instead of choosing 1st element in the s I am converting the entire list into short-form. I was using too complicated a method before I saw Patrick's answer thanks for the suggestion.