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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:58:22+00:00 2026-06-11T22:58:22+00:00

Possible Duplicate: R: losing column names when adding rows to an empty data frame

  • 0

Possible Duplicate:
R: losing column names when adding rows to an empty data frame

I created an empty dataframe with column names only as follows

> compData <- data.frame(A= numeric(0), B= numeric(0))
> compData
[1] A B
<0 rows> (or 0-length row.names)
> compData <- rbind(compData,c(5,443))
> compData
  X5 X443
1  5  443

in the above after adding one row the column names are changed. How can I add new row data to data-frame?

  • 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-11T22:58:24+00:00Added an answer on June 11, 2026 at 10:58 pm

    Adding to a zero-row data.frame will act differently to adding to an data.frame that already contains rows

    From ?rbind

    The rbind data frame method first drops all zero-column and zero-row arguments. (If that leaves none, it returns the first argument with columns otherwise a zero-column zero-row data frame.) It then takes the classes of the columns from the first data frame, and matches columns by name (rather than by position). Factors have their levels expanded as necessary (in the order of the levels of the levelsets of the factors encountered) and the result is an ordered factor if and only if all the components were ordered factors. (The last point differs from S-PLUS.) Old-style categories (integer vectors with levels) are promoted to factors.

    You have a number of options —

    the most straightforward

     compData[1, ] <- c(5, 443)
    

    more complicated

    Or you could coerce c(5,433) to a list or data.frame

    rbind(compData,setNames(as.list(c(5,443)), names(compData)))
    

    or

    rbind(compData,do.call(data.frame,setNames(as.list(c(5,443)), names(compData))))
    

    But in this case you might as well do

    do.call(data.frame,setNames(as.list(c(5,443)), names(compData)))
    

    data.table option

    You could use the data.table function rbindlist which does less checking and thus preserves the names of the first data.frame

    library(data.table)
    rbindlist(list(compData, as.list(c(5,443))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: JavaScript losing “this” object reference with private/public properties Why does the second
Possible Duplicate: Django data migration when changing a field to ManyToMany how I can
Possible Duplicate: How can I combine multiple rows into a comma-delimited list in Oracle?
Possible Duplicate: Resizing an Image without losing any quality The code I always use
Possible Duplicate: Difference of create Index by using include column or not using Edit:
Possible Duplicate: Lost connection to MySQL server during query I am importing some data
Possible Duplicate: Create an empty object in JavaScript with {} or new Object()? When
Possible Duplicate: nth-child for every two table rows I'm trying to work my way
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP

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.