0

In a given string vector i am trying to add a break line("\n") at every character with some conditions(described below). the below string which i am passing

d <- "ABCD CDEFG LM NOPQRSTR";

output expected:

"ABCD\n   //added new break line \n at fourth character which contained space
 CDEF\n   //after the fourth character is C, added a new break line \n
 -G LM\n  //started with hypen(-) continuing with the characters.
 NOPQ\n
 -RSTR"

Condition:

add a new break line i,e "\n" for every 4 characters position if and only based on the below logic
 if the character=""(blank) then
     add break to next line ("\n") at 4th character like above sample output(ABCD\n) reset 
     character continues
else then if character <> "" like (character including number or special character) then
    add break to next line("\n") at 4th character(CDEF\n) along with hypen(-) i,e C in 
    next line 

Hope I made my best to explain the problem. free to write if it is still not understood. Code I tried: I am new to the R world , this is the logic i tried. Please Help

c <- 4  //setting the position index
for (i in 1:nchar(d)){
    //print(substr(d, i,i))
    a<-substr(d, i,c) //get the 4th index
    if(a=""){   //if 4th character is blank
      d<-paste0(a,"\n")  //add a break new line (\n) 
    }else {  
      d<-paste0("-",a)   //if the character contains 4th is a character put that character in 
                           next line continue with -
 }     
}

I am unable return complete string with the breakline adding(\n for every 4th character) and -(if it contains as shown in the sample expected output)

I got the inspirations with the below link , but not able to crack up.

break lines of every string

Thanks in advance

s_baldur
  • 29,441
  • 4
  • 36
  • 69
Rod
  • 75
  • 1
  • 8

1 Answers1

1

With a loop

d <- "ABCD CDEFG LM NOPQRSTR";
dsp <- strsplit(d, '')[[1L]]
step <- 5L
pos <- 5L
while (pos < length(dsp)) {
  if (dsp[pos] == " ") {
    dsp[[pos]] <- '\n'
  } else {
    dsp <- c(dsp[1L:(pos-1L)], "\n-", dsp[-(1:pos-1L)])
  }
  pos <- pos + step
}

cat(paste(dsp, collapse = ""))
# ABCD
# CDEF
# -G LM
# NOPQ
# -RSTR

EDIT:

To return as a column in data.frame (two options):

data.frame(
  x = strsplit(paste(dsp, collapse = ""), split = "\n")[[1]],
  y = strsplit(paste(dsp, collapse = ""), split = "(?<=\n)", perl = TRUE)[[1]]
)

#       x       y
# 1  ABCD  ABCD\n
# 2  CDEF  CDEF\n
# 3 -G LM -G LM\n
# 4  NOPQ  NOPQ\n
# 5 -RSTR   -RSTR
s_baldur
  • 29,441
  • 4
  • 36
  • 69
  • Thanks this is working as excepted , could you help me to write a functions where i am returning this result to a dataframe column. I tried but not able to make it – Rod Oct 22 '21 at 09:42
  • @Rod see update – s_baldur Oct 22 '21 at 09:57
  • thanks once again, but i see we miss to pass the step or pos? stackoverflow.com/questions/51297476/… - just a reference. Can u update function and send – Rod Oct 22 '21 at 18:01
  • I'm not sure I understand. What's the issue? – s_baldur Oct 24 '21 at 09:32
  • the second code you have given, where i can pass the position where this will add "-" or "\n". hope you understood now – Rod Oct 25 '21 at 11:25
  • Not really. Position relative to what? Can you add it to your expected output or perhaps this is too different from the original question that it's better to ask a new one. – s_baldur Oct 25 '21 at 11:32
  • the one you put as step <- 5L pos <- 5L – Rod Oct 25 '21 at 12:31