2010-12-07 8 views
2

は、例えば、データ・セットである:単語の文字列をRの略語に変換し、それを他の行のデータと連結することは可能ですか?ここ

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')) 

そして、私は、例えば、BibTeXの参照を作成するプロセスを自動化したいと思いますこのような機能を持つ:

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

ので、私は次のような結果を得ること:

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

それはRでこれを行うことは可能ですか?

ありがとうございます!

答えて

6

あなたはここでabbreviate

R> abbreviate('test title one two three') 
test title one two three 
      "ttott" 
4

をしたいが、あなたがから構築できる一つの可能​​性は次のとおりです。

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='')) 

これは、少なくとも3つの言葉は、各タイトルであることを前提とした場合に修正する必要がありますそうではありません。

+0

とてもいいです。そのトリックを教えてくれてありがとう! –

関連する問題