Ana içeriğe geç
eLearner.app
Modül 1 · Ders 2 ders 2Kurstaki 2/10~12 min
Modül dersleri (2/2)

Veri Türleri ve Matrisler

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

Egzersiz#r.m1.l2.e1
Denemeler: 0Yükleniyor…

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

Düzenleyici yükleniyor…
İpucu göster

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

3 denemeden sonra çözüm mevcut

Egzersiz#r.m1.l2.e2
Denemeler: 0Yükleniyor…

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

Düzenleyici yükleniyor…
İpucu göster

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

3 denemeden sonra çözüm mevcut

Egzersiz#r.m1.l2.e3
Denemeler: 0Yükleniyor…

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

Düzenleyici yükleniyor…
İpucu göster

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

3 denemeden sonra çözüm mevcut

Egzersiz#r.m1.l2.e4
Denemeler: 0Yükleniyor…

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

Düzenleyici yükleniyor…
İpucu göster

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

3 denemeden sonra çözüm mevcut

Egzersiz#r.m1.l2.e5
Denemeler: 0Yükleniyor…

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

Düzenleyici yükleniyor…
İpucu göster

Use: my_matrix <- cbind(col1, col2)

3 denemeden sonra çözüm mevcut