Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Labeling Vectors | Basic Data Types and Vectors
R Introduction: Part I
course content

Kursusindhold

R Introduction: Part I

R Introduction: Part I

1. Basic Syntax and Operations
2. Basic Data Types and Vectors
3. Factors

book
Labeling Vectors

Now that you're familiar with creating vectors, let's apply them to store and label valuable information, such as grades on a scale from 1 to 100. However, the grades alone may not be self-explanatory to someone else.

Suppose the grades are as follows:

While you know how to create a vector with grades, the next step is labeling those values with their corresponding subjects. This can be done by assigning a vector of names to names(vector), where vector is your original vector that you wish to label.

Here's how you can do that:

1234
grades <- c(80, 75, 95, 100) # Vector of grades # Assign names to values of grades names(grades) <- c('Math', 'Physics', 'English', 'Literature') grades # Output the vector of grades
copy

Here, c('Math', 'Physics', 'English', 'Literature') is the vector of names (labels). As you can see, using this approach, we can present the information more clearly.

Opgave

Swipe to start coding

Imagine you manage a local furniture store. You have a list of items and their respective prices, as shown below:

ItemPrice
Sofa$340
Armchair$150
Dining table$115
Dining chair$45
Bookshelf$160

Your task is to store the prices in a vector named prices and label each price with the corresponding item.

  1. Construct a vector of prices and store it in the variable prices.
  2. Use the variable items to assign names to the elements in the prices vector.
  3. Display the prices vector.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 6
toggle bottom row

book
Labeling Vectors

Now that you're familiar with creating vectors, let's apply them to store and label valuable information, such as grades on a scale from 1 to 100. However, the grades alone may not be self-explanatory to someone else.

Suppose the grades are as follows:

While you know how to create a vector with grades, the next step is labeling those values with their corresponding subjects. This can be done by assigning a vector of names to names(vector), where vector is your original vector that you wish to label.

Here's how you can do that:

1234
grades <- c(80, 75, 95, 100) # Vector of grades # Assign names to values of grades names(grades) <- c('Math', 'Physics', 'English', 'Literature') grades # Output the vector of grades
copy

Here, c('Math', 'Physics', 'English', 'Literature') is the vector of names (labels). As you can see, using this approach, we can present the information more clearly.

Opgave

Swipe to start coding

Imagine you manage a local furniture store. You have a list of items and their respective prices, as shown below:

ItemPrice
Sofa$340
Armchair$150
Dining table$115
Dining chair$45
Bookshelf$160

Your task is to store the prices in a vector named prices and label each price with the corresponding item.

  1. Construct a vector of prices and store it in the variable prices.
  2. Use the variable items to assign names to the elements in the prices vector.
  3. Display the prices vector.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 6
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt