Miha - 3 months ago 7

R Question

So I have vector which contains bibliography

`bibliography <- c("1. Cohen, A. C. (1955). Restriction and selection insamples from bivariate normal distributions. Journal`

of the American Statistical Association, 50, 884–893. 2.Breslow, N. E. and Cain, K. C. (1988). Logistic regression for the two-stage case-control data.

Biometrika, 75, 11–20. 3.Arismendi, J. C. (2013). Multivariate truncated moments. Journal of Multivariate Analysis, 117, 41–75")

I would like to

So far I've tried this (which is not the best option as third bibliograhpy is left out):

`bibliography <- unlist(strsplit(bibliography, " "))`

bibliography <- bibliography[-length(bibliography)] <- paste0(bibliography[-length(bibliography)], ' \\\\ ')

And the output was this (

`[1] "1. Cohen, A. C. (1955). Restriction and selection in samples from bivariate normal distributions. Journal\nof the American Statistical Association, 50, 884–893. \\\\ "`

[2] "2.Breslow, N. E. and Cain, K. C. (1988). Logistic regression for the two-stage case-control data.\nBiometrika, 75, 11–20. \\\\ "

But this is time consuming as I had to manually add double space before every number (i.e., 1. and 2.) for this code to work.

I've also looked here

Add new line before every number in a string

Inserting Newline character before every number occurring in a string?

Answer

This gets you pretty much where you want:

```
library(stringr)
library(dplyr)
# The first line adds the "~" character at the right break point
str_split(gsub("([1-9]\\.[]*[A-Z])","~\\1",bibliography), "~") %>%
unlist() %>%
str_trim(side = c("both")) # Trimming potential spaces at the strings sides
```