Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8519285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:12:25+00:00 2026-06-11T06:12:25+00:00

I am reading data files in text format using readLines . The first ‘column’

  • 0

I am reading data files in text format using readLines. The first ‘column’ is complicated text that I do not need. The next columns contain data that I do need. The first ‘column’ and the data are separated by a colon (:). I wish to split each row at the first colon and delete the resulting text string, keeping only the data.

Below is an example data file. One potential complication is that one line of data contains multiple colons. That line may at some point become my header. So, I probably should not split at every colon, just at the first colon.

my.data <- "first string of text..:  aa : bb : cc 
            next string ........  :   2    0    2
            third string......1990:   7    6    5
            last string           :   4    2    3"

my.data2 <- readLines(textConnection(my.data))
my.data2

I have tried code presented here:

Split on first comma in string

and here:

R: removing the last three dots from a string

Code at the first link above seems to split only at the first colon of the first row. Code at the second link will probably do what I want, but is too complex for me to modify it successfully so far.

Here are the data I hope to obtain, at which point I can simply replace the remaining colons in the first row with empty spaces using a very simple gsub statement:

   aa : bb : cc 
    2    0    2
    7    6    5
    4    2    3

Sorry if this is a duplicate of a post I have not located and thank you for any advice or assistance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:12:27+00:00Added an answer on June 11, 2026 at 6:12 am

    The following will start at the beginning of the string and then grab everything up to and including the first colon and any additional spaces and replace that with nothing (essentially just removing it)

    gsub("^[^:]+:\\s*", "", my.data2)
    

    If you don’t want to remove the spaces you could do

    gsub("^[^:]+:", "", my.data2)
    

    For some clarification on what the original regular expression is doing. Starting at the beginning:

    ^ this says to only find matches at the start of the string

    [^:] this represents any character that is not a colon

    + this says to match the preceding character one or more times (so match as many non-colon characters as possible)

    : this is what actually matches the colon

    \\s this matches a space

    * this says to match the preceding character zero or more times (so we remove any additional space after the colon)

    So putting it all together we start at the beginning of the string then match as many non-colon characters as possible then grab the first colon character and any additional spaces and replace all of that with nothing (essentially removing all of the junk we don’t want).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a project that requires reading text files, extracting data from
I am using a text file that stores 3 columns of data, each having
I have a tab-delimited text file that I am parsing. Its first column contains
I'm reading some data from a file. The format is stated tobe ASCII text
I inherited a data-storage which was using simple text-files to save documents. Documents had
I have a text file which I am reading and storing the data in
I am reading data from a file that has, unfortunately, two types of character
I am reading in data from a file that has three columns. For example
I am reading a std::istream and I need to verify without extracting characters that:
in my program i need to write large text files (~300 mb), the text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.