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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:50:39+00:00 2026-05-28T13:50:39+00:00

Consider the following data in long format library(plyr) library(reshape2) x <- seq(0,2*pi,length=20) ll <-

  • 0

Consider the following data in long format

library(plyr)
library(reshape2)

x <- seq(0,2*pi,length=20)
ll <- ll2 <- list(a = data.frame(x=x, y=sin(x)),
                  b = data.frame(x=x, y=cos(x)))

m <- melt(ll, id="x")
m2 <- m[sample(nrow(m)),]

head(m)
#         x variable     value L1
# 0.0000000        y 0.0000000  a
# 0.3306940        y 0.3246995  a
# 0.6613879        y 0.6142127  a
# 0.9920819        y 0.8371665  a
# 1.3227759        y 0.9694003  a
# 1.6534698        y 0.9965845  a

m$L1 is nicely ordered, but m2$L1 is all over the place. Now, starting with this kind of data, I want to take the difference value[L1 == "b"] - value[L1 == "a"] for each value of x. The following lines illustrate the problem of using diff when m2$L1 is not ordered: the sign can be wrong. Is there a trick I could use to achieve the result of the last two ddply calls, but more elegantly?

res <- ddply(m, "x", summarize, difference = diff(value))
res <- ddply(m2, "x", summarize, difference = diff(value)) # fails, because L1 not ordered
res <- ddply(m2[order(m2$L1, m2$x), ], "x", summarize, difference = diff(value)) 
res <- ddply(m2, "x", function(d)
             data.frame(difference = d$value[d$L1 == "b"] - d$value[d$L1 == "a"]))

plot(res) # visual check of the result
lines(x, cos(x) - sin(x) , col="red")
  • 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-28T13:50:40+00:00Added an answer on May 28, 2026 at 1:50 pm

    Doesn’t dcast do what you want?

    transform( 
      dcast( m2, x + variable ~ L1 ), 
      difference = b - a 
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following data structure: List<Person> People; class Person { List<Car> Cars; List<Hobby> Hobbies;
Consider the following code: client.Send(data, data.Length, endpoint); byte[] response = client.Receive(ref endpoint); While, according
Consider a CSV file with the following data: weight,lat,long 9876,23.44,88.15 1234,27.11,55.33 .... What is
For my question lets consider the following sample table data: ProductID    ProductName    Price   Category 1                Apple                 5.00       Fruits
Consider this scenario: I've an XML file called person.xml with the following data in
Consider the following code and its output: Code #!/usr/bin/perl -w use strict; use Data::Dumper;
Consider the following: package MyApp::CGI; use Moose; use MooseX::NonMoose; use Data::Dumper; extends 'CGI::Application'; BEGIN
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider the following data model: Suppose I have a table called SuperAwesomeData where each
Consider the following two scenarios: //Data Contract public class MyValue { } Scenario 1:

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.