data <- "as follows:\n\n YEAS--231\n\n Adams\n Aguilar\n "
I want the line starting with 2 or less blank space, output like this:
"as follows:"
and remove the lines if they start with 3 or more blank spaces.
text <- str_split(data,"\n")
The type of "text is list, is there a way to convert "text" to character? Or any other ways to get my result?
CodePudding user response:
You can split the string on '\n'
and with grep
return the strings which has less than 3 spaces at the start of the string.
data <- "as follows:\n\n YEAS--231\n\n Adams\n Aguilar\n "
grep('^\\s{3,}', unlist(strsplit(trimws(data), '\n ')), value = TRUE, invert = TRUE)
#[1] "as follows:"
Using stringr
functions -
library(stringr)
library(magrittr)
str_split(trimws(data), '\\n ') %>%
unlist() %>%
str_subset('^\\s{3,}', negate = TRUE)
CodePudding user response:
This is how to start every new line with two blanks instead on any number of blanks so that the lines are aligned:
library(tidyverse)
data <- "as follows:\n\n YEAS--231\n\n Adams\n Aguilar\n "
cat(data)
#> as follows:
#>
#> YEAS--231
#>
#> Adams
#> Aguilar
#>
data %>%
str_replace_all("\n[ ] ", "\n ") %>%
cat()
#> as follows:
#>
#> YEAS--231
#>
#> Adams
#> Aguilar
#>
Created on 2021-09-25 by the reprex package (v2.0.1)