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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:29:10+00:00 2026-06-13T08:29:10+00:00

This is not a complicated problem, and I have a solution, but I can’t

  • 0

This is not a complicated problem, and I have a solution, but I can’t shake the feeling that there is a better way:

I have a data.frame with a tally of successes and opportunities by category, like this:

testFrame <- data.frame(successes = c(100, 150, 18),
                        opportunities = c(215, 194, 40),
                        category = LETTERS[1:3])
testFrame$category <- as.character(testFrame$category)

I want to convert this to a “tall” data.frame, with one column of 1s and 0s indicating success/failure and a second with category labels. I can do this with the following code:

tallFrame <- lapply(1:nrow(testFrame), function(rr){
  cbind(rep(c(1, 0), c(testFrame[rr, "successes"], testFrame[rr, "opportunities"]-testFrame[rr, "successes"])), testFrame[rr, "category"])
  })
tallFrame <- data.frame(do.call(rbind, tallFrame))

The resulting tallFrame is a matrix which I can then convert to a data.frame without any issues, but this seems like a lot of code for a simple task. Surely there is a way to do this more code-efficiently, perhaps with plyr or reshape, or maybe I’m just looking for some code golf.

Thanks in advance.

  • 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-13T08:29:11+00:00Added an answer on June 13, 2026 at 8:29 am

    One does wonder why you need to do this, but regardless…

    A base solution using rep

    with(testFrame, data.frame(category = rep(category, opportunities), 
        indicator =  unlist(mapply(rep, times = c(successes,  opportunities-successes), 
          MoreArgs = list(x = c(0,1))))))
    

    A data.table solution (coding elegance (perhaps a code golf competitor)

    library(data.table)
    DT <- data.table(testFrame)
    DT[,list(indicator = rep(c(0,1), c(successes,  opportunities-successes))), by = category]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This not a programming question but Most of the programmers using Eclipse should have
I have a problem that when I use something like this: const MyList& my_list
So why is this not working? I'm creating a regex that will match a
I want to know i can do something similar to this (not working) code
Well, I have a problem (ok, no real problem, but I wanna try out
Problem background: I have a Qt/QML Symbian application targeting Qt 4.7.4, that requires a
This question is very similar to that posed here . My problem is that
I have a fairly complicated problem. The usual markup (no problems here): <div class=wrapper>
Greetings, here is the problem. I have a page called entList.jsp , that works
I have a complicated SELECT query that filters on a time range, and I

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.