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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:55:59+00:00 2026-05-26T12:55:59+00:00

With df: df <- data.frame(value=abs(rnorm(100, 25, 5)), status=sample(0:1,100,replace=T)) df$value[sample(1:100,5)] <- NA I need to

  • 0

With df:

df <- data.frame(value=abs(rnorm(100, 25, 5)), status=sample(0:1,100,replace=T))
df$value[sample(1:100,5)] <- NA

I need to get a frequency (percentage) table (better return a matrix) like the following:

value | status(0)  status(1)
----------------------------
 <=25 |  23 (23%)   20 (20%)
  >25 |  27 (27%)   25 (25%)
   NA |   3  (3%)    2  (2%)

I can do this using:

br <- seq(0, 50, 25)
with(df, summary(cut(value[status==0], br, labels=br[-1],
     include.lowest=T, ordered_result=T)))
with(df, summary(cut(value[status==1], br, labels=br[-1],
     include.lowest=T, ordered_result=T)))

But would there be a one-time way to return a matrix as above? 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-26T12:56:00+00:00Added an answer on May 26, 2026 at 12:56 pm
    df$value.cut = cut(df$value, breaks=c(0, 25, 100))
    
    > with(df, table(value.cut, status, useNA='ifany'))
              status
    value.cut   0  1
      (0,25]   26 19
      (25,100] 26 24
      <NA>      3  2
    

    (Of course this can be combined into 1 line if you want, but I left it as 2 here for better readability.)

    EDIT: And if you want a table of proportions, formatted as frequencies, you can do:

    df.tab = with(df, table(value.cut, status, useNA='ifany'))
    df.tab[,] = paste(df.tab, ' (', 100*prop.table(df.tab), '%)', sep='')
    
    > df.tab
              status
    value.cut  0        1       
      (0,25]   26 (26%) 19 (19%)
      (25,100] 26 (26%) 24 (24%)
      <NA>     3 (3%)   2 (2%)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

a.2<-sample(1:10,100,replace=T) b.2<-sample(1:100,100,replace=T) a.3<-data.frame(a.2,b.2) r<-sapply(split(a.3,a.2),function(x) which.max(x$b.2)) a.3[r,] returns the list index, not the index for
Let's say I have a data.frame like: a <- c(1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10) df <- data.frame(a,rnorm(100)) And
I have a data frame containing (in random places) a character value (say foo
I'm trying to replace elements of a data.frame containing #N/A with NULL, and I'm
Imagine I have a data frame with 2 columns Id Value 12 13 32
i have a data frame like this A B value 1 1 0.123 2
Possible Duplicate: Extracting indices for data frame rows that have MAX value for named
I want to change the value of the alternate columns of the data frame
Suppose we create a data.frame like this: > hugs_per_day <- rnorm(10) > hugs_per_day <-
I need to sort a data frame by date in R. The dates are

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.