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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:58:15+00:00 2026-05-22T17:58:15+00:00

I have a table that looks like this: And I need it to look

  • 0

I have a table that looks like this:

enter image description here

And I need it to look like this, where net=gross-tare:

enter image description here

How do I do this?


I started by melting the data, then casting as columns, and then creating new columns for the net readings.

  df_m <- melt(df, id = 1:3)
  df_c <- cast(df_m, ... ~ variable + type)

  df_c$wr_net  <- df_c$wr_gross  - df_c$wr_tare
  df_c$wc_net  <- df_c$wc_gross  - df_c$wc_tare
  df_c$tsa_net <- df_c$tsa_gross - df_c$tsa_tare

Which gives

enter image description here

But now I can’t figure out how to melt this table to get the dataframe to look the way I need with a column for ‘type’ with values ‘gross’ and ‘tare’ and ‘net’.

Is there an easier way? Am I barking up the wrong tree with melt/cast?


You can reproduce a small sample of my data using this…

  df <- structure(list(train = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "AC0485n", class = "factor"), 
    position = c(1L, 1L, 2L, 2L, 3L, 3L), type = structure(c(2L, 
    1L, 2L, 1L, 2L, 1L), .Label = c("gross", "tare"), class = "factor"), 
    wids_raw = c(24.85, 146.2, 26.16, 135, 24.7, 135.1), wids_corr = c(26.15, 
    145.43, 27.44, 134.43, 26, 134.52), tsa = c(24.1, 139.2, 
    25, 133.6, 24, 131.1)), .Names = c("train", "position", "type", 
    "wr", "wc", "tsa"), class = "data.frame", row.names = c(NA, 
    -6L))
  • 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-22T17:58:16+00:00Added an answer on May 22, 2026 at 5:58 pm

    I think all you need is to use ddply:

    ddply(df,.(position),.fun=function(x){
    newrow <- x[1,]
    newrow$type <- "net"
    newrow[4:6] <- x[x$type=="gross",4:6] - x[x$type=="tare",4:6]
    return(rbind(x,newrow))
    })
    

    which returns,

       train   position  type     wr     wc   tsa
     1 AC0485n        1  tare  24.85  26.15  24.1
     2 AC0485n        1 gross 146.20 145.43 139.2
     3 AC0485n        1   net 121.35 119.28 115.1
     4 AC0485n        2  tare  26.16  27.44  25.0
     5 AC0485n        2 gross 135.00 134.43 133.6
     6 AC0485n        2   net 108.84 106.99 108.6
     7 AC0485n        3  tare  24.70  26.00  24.0
     8 AC0485n        3 gross 135.10 134.52 131.1
     9 AC0485n        3   net 110.40 108.52 107.1
    

    EDIT:
    And I think this works if you really want to use melt/cast:

    dd <-  melt.data.frame(df_c,id.vars=1:2)
    dd$type <- factor(do.call("rbind",strsplit(as.character(dd$variable),"_"))[,2])
    dd$variable <- factor(do.call("rbind",strsplit(as.character(dd$variable),"_"))[,1])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that looks like this: id value AGA 0.211 AGA 0.433
I have a table that looks like this: CREATE TABLE foobar ( id SERIAL
I have a table that looks like this: ID (pk,int) Col1 (nvarchar) Col2 (nvarchar)
I have a table that looks like this Id FirstName 5 Adam 6 Bob
I have a table that looks like this: table name: uno str_id | title
I have a table that looks like this Group Recipe Priority 0 A 400
I have a table that looks like this: nid vid tid 1 2 3
I have a table that looks like this: <table> <thead> <!-- random table headings
I have a table that looks like this: |StreetName NR| NR | |Teststreet 34|
I have a table that looks like this: id count 1 100 2 50

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.