Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Grouping Numeric Data | Section
Practice
Projects
Quizzes & Challenges
Quizer
Challenges
/
Essential R Programming for Absolute Beginners - 1768563985826

bookGrouping Numeric Data

Continuous numeric data can be transformed into categories using the cut() function. This is helpful when you want to analyze ranges rather than individual values.

Function Overview

The cut() function divides numbers into intervals and returns a factor:

cut(x, breaks, labels = NULL, right = TRUE, ordered_result = FALSE)
  • x: numeric vector to categorize;
  • breaks: number of intervals or specific cut points;
  • labels: names for categories;
  • right: whether intervals are closed on the right;
  • ordered_result: whether the categories should be ordered.

Example

12345678910
heights <- c(170, 165, 195, 172, 189, 156, 178, 198, 157, 182, 171, 184, 163, 176, 169, 153) # Split heights into 3 groups heights_f <- cut(heights, breaks = c(0, 160, 190, 250), labels = c('short', 'medium', 'tall'), ordered_result = TRUE) heights_f
copy

As a result:

  • The data is divided into three intervals: (0,160], (160,190], and (190,250];
  • They are labeled as 'short', 'medium', and 'tall';
  • The categories follow a natural order.
Oppgave

Swipe to start coding

You have a vector of numerical grades. Here's how to categorize them as factor levels:

  • [0, 60) - 'F';
  • [60, 75) - 'D';
  • [75, 85) - 'C';
  • [85, 95) - 'B';
  • [95, 100) - 'A'.

Your task is to:

  1. Create a variable called grades_f that categorizes the grades using the cut() function. Use the following parameters:
    • breaks - c(0, 60, 75, 85, 95, 100);
    • labels - c('F', 'D', 'C', 'B', 'A');
    • ordered_result - TRUE (to order the factor values);
    • right - FALSE (to include the left boundary of an interval, not the right).
  2. Output the contents of grades_f.

Løsning

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 25
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

close

bookGrouping Numeric Data

Sveip for å vise menyen

Continuous numeric data can be transformed into categories using the cut() function. This is helpful when you want to analyze ranges rather than individual values.

Function Overview

The cut() function divides numbers into intervals and returns a factor:

cut(x, breaks, labels = NULL, right = TRUE, ordered_result = FALSE)
  • x: numeric vector to categorize;
  • breaks: number of intervals or specific cut points;
  • labels: names for categories;
  • right: whether intervals are closed on the right;
  • ordered_result: whether the categories should be ordered.

Example

12345678910
heights <- c(170, 165, 195, 172, 189, 156, 178, 198, 157, 182, 171, 184, 163, 176, 169, 153) # Split heights into 3 groups heights_f <- cut(heights, breaks = c(0, 160, 190, 250), labels = c('short', 'medium', 'tall'), ordered_result = TRUE) heights_f
copy

As a result:

  • The data is divided into three intervals: (0,160], (160,190], and (190,250];
  • They are labeled as 'short', 'medium', and 'tall';
  • The categories follow a natural order.
Oppgave

Swipe to start coding

You have a vector of numerical grades. Here's how to categorize them as factor levels:

  • [0, 60) - 'F';
  • [60, 75) - 'D';
  • [75, 85) - 'C';
  • [85, 95) - 'B';
  • [95, 100) - 'A'.

Your task is to:

  1. Create a variable called grades_f that categorizes the grades using the cut() function. Use the following parameters:
    • breaks - c(0, 60, 75, 85, 95, 100);
    • labels - c('F', 'D', 'C', 'B', 'A');
    • ordered_result - TRUE (to order the factor values);
    • right - FALSE (to include the left boundary of an interval, not the right).
  2. Output the contents of grades_f.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 25
single

single

some-alt