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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:37:45+00:00 2026-06-13T16:37:45+00:00

Hi I have a utility function I’ve put together to insert rows into a

  • 0

Hi I have a utility function I’ve put together to insert rows into a dataframe below. If I was writing out the formula by hand I would put something like

newframe=rbind(oldframe[1:rownum,],row_to_insert=row_to_insert,oldframe[(rownum+1:nrow(oldframe),] to name row_to_insert. Could someone tell me how to do this in a function?

Thanks

insertrows=function (x, y, rownum) 
{
    newframe = rbind(y[1:rownum, ], x, y[(rownum + 1):nrow(y), 
        ])
    return(data.frame(newframe))
}

MWE of some underlying data added below

financials=data.frame(sales=c(100,150,200,250),some.direct.costs=c(25,30,35,40),other.direct.costs=c(15,25,25,35),indirect.costs=c(40,45,45,50))

oldframe=t(financials)
colnames(oldframe)=make.names(seq(2000,2003,1))

total.direct.costs=oldframe['some.direct.costs',]+oldframe['other.direct.costs',]
newframe=total.direct.costs

n=rownum=3
oldframe=insertrows(total.direct.costs=newframe,oldframe,n)
  • 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-13T16:37:46+00:00Added an answer on June 13, 2026 at 4:37 pm

    Try this modified function:

    insertrows <- function (x, oldframe, rownum) 
    {
      newframe <- rbind(oldframe, x)
      rownames(newframe) <- c(rownames(oldframe),  deparse(substitute(x)))
      return(newframe[c(seq(n), nrow(oldframe) + 1, seq(n + 1, nrow(oldframe))), ])
    }
    

    Now, the name of the object which is passed as argument x will be used as the rowname of the new row (deparse(substitute(x))). The old rownames are kept. Furthermore, the ordering of the rows is now more efficient.

    Run the function:

    insertrows(total.direct.costs, oldframe, 3)
    

    The output:

                       [,1] [,2] [,3] [,4]
    sales               100  150  200  250
    some.direct.costs    25   30   35   40
    other.direct.costs   15   25   25   35
    total.direct.costs   40   55   60   75
    indirect.costs       40   45   45   50
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a utility function in my program for searching for entities. It takes
Suppose I have a utility function like this - public static boolean isABlankSpace(char c)
I have created an utility function which I include in almost all Javascript code
I have tried to write an utility function that can convert my custom data
I am writing a little utility function in Python which returns a boolean, indicating
I have a multi-threading Python program, and a utility function, writeLog(message) , that writes
I have a utility function that will shuffle the elements of any Vector, but
I'd like to have a utility function that conditionally updates my request and response
I have written a utility function which expects the window object to work on.
I've created a small utility function for string conversion so that I don't 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.