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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:42:26+00:00 2026-06-17T14:42:26+00:00

I want to use stack to a data frame and keep duplicate columnnames from

  • 0

I want to use stack to a data frame and keep duplicate columnnames from a original data frame.

a1<-data.frame(1:10,11:20,21:30)
colnames(a1)<-c('a','b','a')
a2<-stack(a1)

Above adds ‘a.1’ for duplicate column names for ‘a’ in a2. I want to keep it as ‘a’. Is there any option for this in stack?

Alternatively I tried to replace all ‘a1.1’ using a loop. I wasn’t successful in the large table with many duplicate names. Is there any better example for replacing the pattern (strings) in r rows?

Finally, I am here asking my first question in forum. Thanks for your help.

  • 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-17T14:42:28+00:00Added an answer on June 17, 2026 at 2:42 pm

    By looking at the code for stack by:

    > getS3method('stack', 'default')
    
    function (x, ...) 
    {
        x <- as.list(x)
        keep <- unlist(lapply(x, is.vector))
        if (!sum(keep)) 
            stop("at least one vector element is required")
        if (!all(keep)) 
            warning("non-vector elements will be ignored")
        x <- x[keep]
        data.frame(values = unlist(unname(x)), ind = factor(rep.int(names(x), 
            lapply(x, length))), stringsAsFactors = FALSE)
    }
    

    As you see the ... parameter is not passed to the data.frame creation. You could modify this function by creating say stack2 as follows:

    stack2 <- function (x, ...) 
    {
        x <- as.list(x)
        keep <- unlist(lapply(x, is.vector))
        if (!sum(keep)) 
            stop("at least one vector element is required")
        if (!all(keep)) 
            warning("non-vector elements will be ignored")
        x <- x[keep]
        data.frame(values = unlist(unname(x)), ind = factor(rep.int(names(x), 
            lapply(x, length))), stringsAsFactors = FALSE, ...) # note the ... here
    }
    # after copy/paste of stack2 function
    > stack2(a1, check.names = FALSE)
       values ind
    1       1   a
    2       2   a
    3       3   a
    4       4   a
    5       5   a
    6       6   a
    7       7   a
    8       8   a
    9       9   a
    10     10   a
    11     11   b
    12     12   b
    13     13   b
    14     14   b
    15     15   b
    16     16   b
    17     17   b
    18     18   b
    19     19   b
    20     20   b
    21     21   a
    22     22   a
    23     23   a
    24     24   a
    25     25   a
    26     26   a
    27     27   a
    28     28   a
    29     29   a
    30     30   a
    

    Note: Don’t pass stringsAsFactors = . argument to this stack2 function as its already passed. Probably that’s why they don’t pass the ... argument?

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

Sidebar

Related Questions

i want use some data from a website with web service. i have a
Are there any cases when I would want to use an explicit stack data-structure
I want to use Core Data in background threads and NSOperationQueue. Now I thought
so I want to use this JQuery plugin that Stack Overflow has made available
I need a Stack data structure for my use case. I should be able
I am currently compiling a bought data stack in C. I use their own
I have use java stack data structure to maintain data. I have limit my
I want use this 1 for using Bar code or QR code scanner. I
I want use BYTE_ORDER macro in my Xcode project but i can't because i
I want use javascript setInterval function to achieve a box rotate animate effect, I

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.