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

  • SEARCH
  • Home
  • 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 6914055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:17:54+00:00 2026-05-27T09:17:54+00:00

I am trying to use colsplit to break up a vector in a dataframe.

  • 0

I am trying to use colsplit to break up a vector in a dataframe. The fact that we have regular expression as an arg to colsplit makes me think it can be flexible, but I am having trouble (it might just be that I’m not understanding regex in R).

Here’s the problem:

let’s create a vector…

> library(reshape)
> my_var_1 <- factor(c("x00_aaa_123","x00_bbb_123","x00_ccc_123","x01_aaa_123","x01_bbb_123","x01_ccc_123","x02_aaa_123","x02_bbb_123","x02_ccc_123"))

I would like to split it into two columns upon the first underscore.
In other words, I want my end result to be this…

    x whatever
1 x00  aaa_123
2 x00  bbb_123
3 x00  ccc_123
4 x01  aaa_123
5 x01  bbb_123
6 x01  ccc_123
7 x02  aaa_123
8 x02  bbb_123
9 x02  ccc_123

I am trying to find the right regex inside of colspan that will do it, but no luck. Here’s the closest I can get…

> colsplit(my_var_1, split="_", c("x","whatever")) 
    x whatever NA.
1 x00      aaa 123
2 x00      bbb 123
3 x00      ccc 123
4 x01      aaa 123
5 x01      bbb 123
6 x01      ccc 123
7 x02      aaa 123
8 x02      bbb 123
9 x02      ccc 123

That uses the split regex as a simple delimiter and it gives me three columns. I would like to not split the second underscore (to make it worse, in my real data I have an arbitrary number of underscores not just two).

Is there an expression I can use for “split” that will give what I want?

I had hoped that the regex in colsplit would allow me to match on groups and the group matches would be the content of splits but that does not appear to be the case.

* edit (thanks to @Joshuaulrich) colsplit works “as intended” when using the newer reshape2 !!!

  • 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-05-27T09:17:55+00:00Added an answer on May 27, 2026 at 9:17 am

    Your code throws an error for me:

    > colsplit(my_var_1, split="_", c("x","whatever"))
    Error in colsplit(my_var_1, split = "_", c("x", "whatever")) : 
      unused argument(s) (split = "_")
    

    split isn’t an argument to colsplit. The argument you want is pattern, or you can just rely on positional matching:

    > colsplit(my_var_1, "_", c("x","whatever"))
        x whatever
    1 x00  aaa_123
    2 x00  bbb_123
    3 x00  ccc_123
    4 x01  aaa_123
    5 x01  bbb_123
    6 x01  ccc_123
    7 x02  aaa_123
    8 x02  bbb_123
    9 x02  ccc_123
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a regex that I'm trying use to validate against strings. Trying to
I have a 3rd party DLL that I am trying to use in a
I'm trying use regular expression to convert special characters in an url. Here's my
I'm trying use eco for client-side templating. I have multiple .eco templates that I'd
I am trying use javascript regular expressions to do some matching and I found
I have been trying use the edit_post_link() function to contain an image. All of
I am trying use Thread but i have some problem (I am beginner at
Trying to use the getDependentRowset in a ZF application. I have the following relationship
I am trying use the jQuery table sorter plugin for a table that is
I am trying use neo4j with Ruby on rails on Mac OSX. I have

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.