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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:10:58+00:00 2026-06-04T03:10:58+00:00

I just discovered the great plyr package and am taking it for a spin.

  • 0

I just discovered the great plyr package and am taking it for a spin.

A question I have is the following: is there some way to access the grouping variables from within d_ply?

Say I have a dataframe df with columns x,y,z, and I would like to plot for each z the values x versus y. If I do the following:

plotxy = function(df, ...) {plot(df$x, df$y, ...)}
d_ply(df, .(z), plotxy(df, main=.(z)))

then the titles that show up on the plots are all “z”, and not the values of the z variable. Is there a way to access those values from within d_ply?

EDIT: As @Justin pointed out, the above formulation is wrong because I am passing the whole of df to plotxy. Hence the line

d_ply(df, .(z), plotxy(df, main=.(z)))

should be

d_ply(df, .(z), plotxy, main=.(z))

in order to make sense in terms of my original question (I guess that’s also what @joran was hinting at).

However, I realized something else. Even though df gets sliced along z by d_ply, the sub-dataframe that the function receives still has a z column — simply with always the same value. Hence the problem can apparently be solved as follows:

plotxy = function(df, ...) {plot(df$x, df$y, main=df$z[1])}
d_ply(df, .(z), plotxy)
  • 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-04T03:11:00+00:00Added an answer on June 4, 2026 at 3:11 am

    By way of example, I’ll expand on Joran’s concern.

    df <- data.frame(x=rnorm(100), y=rnorm(100), z=letters[1:10])
    

    lets use your function and see what we get without plyr:

    plotxy(df, main=.(z))
    

    versus the maybe more expected(?):

    plotxy(df, main=df$z)
    

    However, in you code, you are splitting your data frame on z then sending the whole data.frame df to your function again. Instead you could make a wrapper function:

    d_ply(df, .(z), function(ply.df) plotxy(ply.df, main=unique(ply.df$z)))
    

    This way the plotxy function is only seeing the smaller split data.frame ply.df that you pass through the wrapper function.

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

Sidebar

Related Questions

I just discovered the what c# knowledge should I have? question and wondered about
I just discovered there is a Ajax success handler, which is great because its
I just discovered ShareKit , and it looks great. I have one doubt, though:
I'm just starting out with MS Access 2010 and have the following setup. 3
i just discovered http://code.google.com/p/re2 , a promising library that uses a long-neglected way (
I recently found SOAPUI and discovered that it is just a great tool for
Just discovered Node.io , gone though the docs, api, etc. and it looks great.
I've just discovered TideSDK and it seems to be a really great tool, but
I am trying to get inheritance in JS. I just discovered a neat way
I just discovered this great site. I was reading bash tagged posts when the

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.