Skip to content

Instantly share code, notes, and snippets.

@danlewer
Last active July 30, 2024 10:45
Show Gist options
  • Save danlewer/309b7c6d37b4df8fa5cb1ef54425429d to your computer and use it in GitHub Desktop.
Save danlewer/309b7c6d37b4df8fa5cb1ef54425429d to your computer and use it in GitHub Desktop.
Convert RIS to CSV (requires data.table)
# Reads a RIS file and converts it into a 'wide' data.table
# Where records have multiple entries for a field, eg. author or keyword, these are given numbered column names, eg. KW1, KW2 etc
# Note if the field label already has a number, this will be maintained, eg. T1 (for title) becomes T11
library(data.table)
ris2csv <- function (file) { # 'file' is the ris file in your working directory, eg. 'myrisfile.ris'
d <- readLines(file)
d <- strsplit(d, " - ", fixed = T)
d[sapply(d, length) == 0] <- 'NEW RECORD'
d <- do.call(rbind, d)
d <- data.table(d)
names(d) <- c('field', 'value')
d$id <- cumsum(d$field == 'NEW RECORD')
d$fieldid <- paste0(d$field, rowid(d$id, d$field))
d <- d[field != 'NEW RECORD']
dcast(d, id ~ fieldid, value.var = 'value')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment