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

The Archive Base Latest Questions

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

I have a dataset with about 3 million rows and the following structure: PatientID|

  • 0

I have a dataset with about 3 million rows and the following structure:

PatientID| Year | PrimaryConditionGroup
---------------------------------------
1        | Y1   | TRAUMA
1        | Y1   | PREGNANCY
2        | Y2   | SEIZURE
3        | Y1   | TRAUMA

Being fairly new to R, I have some trouble finding the right way to reshape the data into the structure outlined below:

PatientID| Year | TRAUMA | PREGNANCY | SEIZURE
----------------------------------------------
1        | Y1   | 1      | 1         | 0
2        | Y2   | 0      | 0         | 1
3        | Y1   | 1      | 0         | 1

My question is: What is the fastest/most elegant way to create a data.frame, where the values of PrimaryConditionGroup become columns, grouped by PatientID and Year (counting the number of occurences)?

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

    There are probably more succinct ways of doing this, but for sheer speed, it’s hard to beat a data.table-based solution:

    df <- read.table(text="PatientID Year  PrimaryConditionGroup
    1         Y1    TRAUMA
    1         Y1    PREGNANCY
    2         Y2    SEIZURE
    3         Y1    TRAUMA", header=T)
    
    library(data.table)
    dt <- data.table(df, key=c("PatientID", "Year"))
    
    dt[ , list(TRAUMA =    sum(PrimaryConditionGroup=="TRAUMA"),
               PREGNANCY = sum(PrimaryConditionGroup=="PREGNANCY"),
               SEIZURE =   sum(PrimaryConditionGroup=="SEIZURE")),
       by = list(PatientID, Year)]
    
    #      PatientID Year TRAUMA PREGNANCY SEIZURE
    # [1,]         1   Y1      1         1       0
    # [2,]         2   Y2      0         0       1
    # [3,]         3   Y1      1         0       0
    

    EDIT: aggregate() provides a ‘base R’ solution that might or might not be more idiomatic. (The sole complication is that aggregate returns a matrix, rather than a data.frame; the second line below fixes that up.)

    out <- aggregate(PrimaryConditionGroup ~ PatientID + Year, data=df, FUN=table)
    out <- cbind(out[1:2], data.frame(out[3][[1]]))
    

    2nd EDIT Finally, a succinct solution using the reshape package gets you to the same place.

    library(reshape)
    mdf <- melt(df, id=c("PatientID", "Year"))
    cast(PatientID + Year ~ value, data=j, fun.aggregate=length)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple compound dataset, about 1 million rows of a compound
I have a rather large dataset in text file with about 25 million rows
I have a dataset with ~20M rows and I'm observing the following behavior. The
I have a dataset about maximum wind speed(cm) as below: Year Jan Feb Mar
I have a large dataset, over 1.5 million rows, from 600k unique subjects, so
I have a dataset of about 3000 rows in openoffice, each set MAY contain
I have a dataset of the following structure: 1234 4334 8677 3753 3453 4554
We have a Dataset with about 40 TableAdapters on it. These are used everywhere
I have a collection with about 500000 dataset in it and I like to
I have a very large database with about 120 Million records in one table.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.