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 8571711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:50:29+00:00 2026-06-11T18:50:29+00:00

Is it possible to change the default separator when cast (dcast) assigns new column

  • 0

Is it possible to change the default separator when cast (dcast) assigns new column headers?

I am converting a file from long to wide, and I get the following headers:

value_1, value_2, value_3,...  

In reshape you can assign the “sep” parameter (sep=””) and the column headers output like I want them to:

value1, value2, value3,... 

However, reshape takes minutes for my data frame with over 200,000 rows, whereas dcast takes seconds. dcast also outputs the columns in the order I want, where reshape does not. Is there any easy way to change the output with dcast, or do I need to change the column headers manually?

For example:

example <- data.frame(id=rep(c(1,2,3,4),4),index=c(rep(1,4),rep(2,4),rep(1,4),rep(2,4)),variable=c(rep("resp",8),rep("conc",8)),value=rnorm(16,5,1))
dcast(example,id~variable+index)

The example gives the column headers:

conc_1, conc_2, resp_1, resp_2

I want the column headers to read:

conc1, conc2, resp1, resp2

I have tried:

dcast(example,id~variable+index,sep="")

dcast appears to ignore sep entirely, because giving a symbol does not change the output either.

  • 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-11T18:50:30+00:00Added an answer on June 11, 2026 at 6:50 pm

    You can’t, since that option wasn’t incorporated into dcast. But it’s fairly trivial to do this after running dcast.

    casted_data <- dcast(example,id~variable+index)
    
    
    library(stringr)
    names(casted_data) <- str_replace(names(casted_data), "_", ".")
    
    > casted_data
      id   conc.1   conc.2   resp.1   resp.2
    1  1 5.554279 5.225686 5.684371 5.093170
    2  2 4.826810 5.484334 5.270886 4.064688
    3  3 5.650187 3.587773 3.881672 3.983080
    4  4 4.327841 4.851891 5.628488 4.305907
    
    # If you need to do this often, just wrap dcast in a function and 
    # change the names before returning the result.
    
    f <- function(df, ..., sep = ".") {
        res <- dcast(df, ...)
        names(res) <- str_replace(names(res), "_", sep)
        res
    }
    
    > f(example, id~variable+index, sep = "")
      id   conc1   conc2   resp1   resp2
    1  1 5.554279 5.225686 5.684371 5.093170
    2  2 4.826810 5.484334 5.270886 4.064688
    3  3 5.650187 3.587773 3.881672 3.983080
    4  4 4.327841 4.851891 5.628488 4.305907
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to change the default field separator from comma to to some
Is it possible to change the default collation based on a column? i want
Is it possible to change the default name of the file gmon.out, which is
Is it possible to change default system's 'hand' cursor with some 'my_cursor.png file in
Is it possible to change the default text (something like looking for other iPhones
Is it possible to change the default background colour of black in Silverlight Encoder
I'm trying to know whether it is possible to change the default android OS
In Ruby on Rails, is it possible to change a default action for a
Possible Duplicate: Change the URL in the browser without loading the new page using
I was wondering if its possible to change the default program class that gets

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.