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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:31:00+00:00 2026-05-25T20:31:00+00:00

Given the following data.frame: t x y ——— 1 1 3 1 1 3

  • 0

Given the following data.frame:

t   x   y
---------
1   1   3
1   1   3
1   1   2
2   1   2
2   2   2

I would like output of the form

t   cnt cux cuy
---------------
1   3   1   2
2   2   2   1

where cnt is the count of all rows with a particular value t, cux/cuy is the count of all unique rows of x/y

The other constraint is that the answer must work for a variable number of columns.

Thanks.

  • 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-25T20:31:00+00:00Added an answer on May 25, 2026 at 8:31 pm

    What you describe in words and what you show in expected output do not agree. In particular, counting unique values of y would be 2 and 1, not 3 and 2 based on your input. Going with the written description:

    DF <- data.frame(t=c(1,1,1,2,2), x=c(1,1,1,1,2), y=c(3,3,2,2,2))
    
    library("plyr")
    
    ddply(DF, .(t), function(DF) {
        data.frame(cnt=length(DF$t), colwise(function(x) {length(unique(x))})(DF))
    })
    

    Or if you want something really functional looking:

    library("functional")
    
    ddply(DF, .(t), function(DF) {
        data.frame(cnt=length(DF$t), colwise(Compose(unique, length))(DF))
    })
    

    Or going completely overboard with the functional paradigm:

    merge(ddply(DF, .(t), summarise, cnt=length(t)),
        ddply(DF, .(t), colwise(Compose(unique, length))))
    

    None of these give the column names you asked for; instead of cux it is x. However, they can be changed afterward.

    res <-
    merge(ddply(DF, .(t), summarise, cnt=length(t)),
        ddply(DF, .(t), colwise(Compose(unique, length))))
    
    names(res)[-(1:2)] <- paste("cu", names(DF)[-1], sep="")
    

    which gives

    > res
      t cnt cux cuy
    1 1   3   1   2
    2 2   2   2   1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following schema / data / output how would I format a SQL
I'm looking to get a count for the following data frame: > Santa Believe
Given is the following data frame: structure(list(UH6401 = c(1, 1, 0, 0, 0, 1,
I have a data frame that has a format like the following: Month Frequency
I have an odd linq subquery issue. Given the following data structure: Parents Children
Given the following example data: Users +--------------------------------------------------+ | ID | First Name | Last
Given the following simple BST definition: data Tree x = Empty | Leaf x
How can you sort the following word by the given rule? Example data is
Hey guys, given a data set in plain text such as the following: ==Events==
// given following array: $data = array( 0=>array( data=>object1, col=>array( 0=>array( data=>object2, col=>array( 0=>array(

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.