דילוג לתוכן המרכזי
eLearner.app
מודול 1 · שיעור 2 מתוך 22/10 בקורס~12 min
שיעורי מודול (2/2)

סוגי נתונים ומטריצות

R has several basic data types that allow storing different information inside vectors and variables.

Elementary Data Types

The three main data types used most frequently in R are:

  1. numeric: Decimal numbers (e.g., 4.5, 10.0) or integers.
  2. character: Text strings enclosed by double or single quotes (e.g., "Alice", 'Data').
  3. logical: Boolean values, which can only be TRUE or FALSE. In R, you can also use the abbreviations T or F (though it is recommended to use the full versions for clarity).
Code
# Examples of different types
score <- 95.5          # numeric
player_name <- "Marco"  # character
is_winner <- TRUE       # logical

Matrices

A matrix in R is a two-dimensional extension of a vector. Like vectors, matrices can only contain elements of a single type (usually numbers).

You create a matrix using the matrix() function, providing a vector of data and specifying the number of rows (nrow) or columns (ncol):

Code
# Create a 2x3 matrix with values from 1 to 6
mat <- matrix(1:6, nrow = 2, ncol = 3)
print(mat)

By default, R populates the matrix column by column.

Accessing Elements

To access a specific element in a matrix, use square brackets [row, column]:

Code
# Extract the element at the first row, second column
value <- mat[1, 2]

# Extract an entire row by leaving the column field empty
first_row <- mat[1, ]

# Extract an entire column
second_col <- mat[, 2]

Factors

Factors in R are special data structures used to represent categorical variables (qualitative data that belong to a finite set of categories, such as gender or education level). They are highly important in statistical analysis:

Code
sizes <- c("M", "L", "M", "S")
size_factor <- factor(sizes)
print(size_factor) # Also shows the Levels: S M L

Combining Data: cbind and rbind

We can combine vectors or matrices by binding them by column with cbind() (column bind) or by row with rbind() (row bind):

Code
v1 <- c(1, 2)
v2 <- c(3, 4)

# Creates a 2x2 matrix by placing v1 and v2 side-by-side as columns
combined <- cbind(v1, v2)

Try it yourself

פעילות גופנית#r.m1.l2.e1
ניסיונות: 0טוען...

Declare a logical variable named is_active set to TRUE and a character variable named user_name set to 'Alice'.

טוען עורך...
הצג רמז

Use the <- operator for both variables on separate lines.

הפתרון זמין לאחר 3 ניסיונות

פעילות גופנית#r.m1.l2.e2
ניסיונות: 0טוען...

Create a matrix named mat containing the values from 1 to 6, structured with 2 rows (nrow = 2) and 3 columns (ncol = 3).

טוען עורך...
הצג רמז

Use the matrix(1:6, nrow = 2, ncol = 3) function.

הפתרון זמין לאחר 3 ניסיונות

פעילות גופנית#r.m1.l2.e3
ניסיונות: 0טוען...

Given the already created matrix mat, extract the element located at the first row and third column, and save it in the variable val.

טוען עורך...
הצג רמז

Access the element using square brackets: val <- mat[1, 3]

הפתרון זמין לאחר 3 ניסיונות

פעילות גופנית#r.m1.l2.e4
ניסיונות: 0טוען...

Create a factor from the vector categories and assign it to the variable cat_factor.

טוען עורך...
הצג רמז

Use the factor() function: cat_factor <- factor(categories)

הפתרון זמין לאחר 3 ניסיונות

פעילות גופנית#r.m1.l2.e5
ניסיונות: 0טוען...

Given the vectors col1 and col2, combine them as columns of a matrix using cbind() and assign the result to my_matrix.

טוען עורך...
הצג רמז

Use: my_matrix <- cbind(col1, col2)

הפתרון זמין לאחר 3 ניסיונות