0

I am working on the following problem in R

[ Note: I realize that there is an answer posted to a similar question which is

How do I get the length of the first line in a multi line string?

however I am looking for a solution in R, not in python. ]

Suppose I have a string on multiple lines such as the one below:

"The first line of the string,
the second line of the string
and finally the the third line of the string."

I would like to know the length of just the first line of the string which is:

> nchar("The first line of the string,")
[1] 29

However the string is really stored as

> "The first line of the string,
+ the second line of the string
+ and finally the the third line of the string"
[1] "The first line of the string,\nthe second line of the string\nand finally the the third line of the string"

So when I apply the nchar function to the entire string, I get

> my_string = "The first line of the string,
+ the second line of the string
+ and finally the the third line of the string"
> nchar(my_string)
[1] 104

which is not correct.

Is there a way I can get the number of characters on just the first line?

NM_
  • 1,887
  • 3
  • 12
  • 27

3 Answers3

1

I found one solution:

nchar(strsplit(my_string, "\n")[[1]])

Maybe a better one exists.

NM_
  • 1,887
  • 3
  • 12
  • 27
1

baseR solution

nchar( gsub( "(^[^\n]*).*$", "\\1", my_string) )

takes the part from the start of the string, untill the first \n (=newline character)

Wimpel
  • 26,031
  • 1
  • 20
  • 37
1

You can use sub to remove everything after \n and use then nchar.

nchar(sub("\n.*", "", my_string))
#[1] 29

or using strsplit

nchar(strsplit(my_string, "\n")[[1]][1])
#nchar(strsplit(my_string, "\n")[[c(1,1)]]) #Alternative
#[1] 29
GKi
  • 37,245
  • 2
  • 26
  • 48