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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:22:34+00:00 2026-06-01T17:22:34+00:00

Consider that I have the following data and function returning summary statistics that I

  • 0

Consider that I have the following data and function returning summary statistics that I like

landlines <- data.frame(
                year=rep(c(1990,1995,2000,2005,2010),times=3),
                country=rep(c("US", "Brazil", "Asia"), each=5),
                pct =  c(0.99, 0.99, 0.98, 0.05, 0.9,
                         0.4,  0.5,  0.55, 0.5,  0.45,
                         0.7,  0.85, 0.9,  0.85, 0.75)
                )
someStats <- function(x)
{
  dp <- as.matrix(x$pct)-mean(x$pct)
  indp <- as.matrix(x$year)-mean(x$year)
  f <- lm.fit( indp,dp )$coefficients
  w <- sd(x$pct)
  m <- min(x$pct)
  results <- c(f,w,m)
  names(results) <- c("coef","sdev", "minPct")
  results
}

I can apply that function to a data subset successfully like this:

> someStats(landlines[landlines$country=="US",])
      coef      sdev    minPct 
 -0.022400  0.410938  0.050000 

or look at a breakdown by country like this:

> by(landlines, list(country=landlines$country), someStats)
country: Asia
      coef       sdev     minPct 
0.00200000 0.08215838 0.70000000 
--------------------------------------------------------------------------------------- 
country: Brazil
      coef       sdev     minPct 
0.00200000 0.05700877 0.40000000 
--------------------------------------------------------------------------------------- 
country: US
     coef      sdev    miPct 
-0.022400  0.410938  0.050000 

Trouble is, that is not the data.frame object I need for further processing, and it won’t cast as such:

> as.data.frame( by(landlines, list(country=landlines$country), someStats) )
Error in as.data.frame.default(by(landlines, list(country = landlines$country),  : 
  cannot coerce class '"by"' into a data.frame

“No problem!” I think, since the similar aggregate() function does return a data.frame:

> aggregate(landlines$pct, by=list(country=landlines$country), min)
  country    x
1    Asia 0.70
2  Brazil 0.40
3      US 0.05

Trouble is, it doesn’t work properly with arbitrary functions:

> aggregate(landlines, by=list(country=landlines$country), someStats)
Error in x$pct : $ operator is invalid for atomic vectors

What I really want to get is a data.frame object with the following columns:

  • country
  • coef
  • sdev
  • minPct

How can I do that?

  • 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-01T17:22:36+00:00Added an answer on June 1, 2026 at 5:22 pm

    take a look at the plyr package and in particular ddply

    > ddply(landlines, .(country), someStats)
      country    coef       sdev minPct
    1    Asia  0.0020 0.08215838   0.70
    2  Brazil  0.0020 0.05700877   0.40
    3      US -0.0224 0.41093795   0.05
    

    Ideally your function explicitly returns a data.frame but in this case, it can be coerced to one easily and correctly.

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

Sidebar

Related Questions

Lets consider that I have a public property called AvatarSize like the following, public
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider the following scenario. We have a C++ function with a static local variable:
Consider that I have the following javascript that do a post: $.post("/MyController/SomeAction", { myParam:
Consider the following function using jQuery: function getVal() { jQuery.get('/relative/url/', function (data) { return
Consider that I have a transaction: BEGIN TRANSACTION DECLARE MONEY @amount SELECT Amount AS
Consider that I have 1 resource and 2 urls (let's say new one and
I have a chat application(Socket Programming) , for that consider I have 2 separate
Assume that we have multiple arrays of integers. You can consider each array as
I have a configuration file that I consider to be my base configuration. I'd

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.