Scalars and Vectors in R

Scalars and Vectors in R

Scalars and Vectors

Scalars

In R, a “scalar” is essentially a single value. Although R does not have a distinct scalar type (as in some other programming languages), a scalar in R is effectively a vector of length 1.

Examples of Scalars:

  • Numeric: 42, 3.14
  • Character: “Hello”, “R”
  • Logical: TRUE, FALSE

Creating Scalars: 

# Numeric
num_scalar <- 42
# Character
char_scalar <- "Hello"
# Logical
log_scalar <- TRUE

 Properties of Scalars:

  • Length: The length() function returns 1 for a scalar.
  • Class: The class() function indicates the data type of the scalar. 
length(num_scalar)  # returns 1
class(num_scalar)   # returns "numeric"
length(char_scalar) # returns 1
class(char_scalar)  # returns "character"
length(log_scalar)  # returns 1
class(log_scalar)   # returns "logical"

 Vectors

Vectors in R are collections of elements of the same type. They are one-dimensional and can contain multiple scalars. Here are the most common types of vectors:

  • Numeric Vectors: Contain numbers (integers or real numbers).
  • Logical Vectors: Contain boolean values (TRUE or FALSE).
  • Character Vectors: Contain strings.
  • Complex Vectors: Contain complex numbers (with real and imaginary parts).

Creating Vectors:

  • Numeric Vector: Use the c() function to combine several numbers. 
num_vector <- c(1, 2, 3, 4, 5)
  • Logical Vector: Combine several boolean values. 
log_vector <- c(TRUE, FALSE, TRUE)
  • Character Vector: Combine several strings. 
char_vector <- c("a", "b", "c")
  • Complex Vector: Create a vector of complex numbers. 
complex_vector <- c(1+2i, 3+4i)

Properties of Vectors:

  • Length: The length() function returns the number of elements in a vector. 
length(num_vector)  # returns 5
  • Class: The class() function indicates the type of vector. 
class(num_vector)   # returns "numeric"
class(char_vector)  # returns "character"
  • Indexing: You can access elements of a vector by their index. Indexing starts at 1 in R.
# Access the 3rd element
num_vector[3]  # returns 3
# Access elements 1 and 3
num_vector[c(1, 3)]  # returns c(1, 3)

Manipulating Vectors:

  • Adding Elements: Combine the existing vector with new elements using c().
num_vector <- c(num_vector, 6, 7)  # vector becomes c(1, 2, 3, 4, 5, 6, 7)
  • Removing Elements: Use negative indices to exclude certain elements.
num_vector <- num_vector[-3]  # removes the 3rd element, becomes c(1, 2, 4, 5, 6, 7)
  • Modifying Elements: Assign a new value to one or more elements.
num_vector[2] <- 20  # vector becomes c(1, 20, 4, 5, 6, 7)

Operations on Vectors:

  • Arithmetic Operations: Arithmetic operations are performed element-wise.
num_vector + 10  # returns c(11, 30, 14, 15, 16, 17)
  • Statistical Functions: Use functions like mean(), sum(), median(), etc.
mean(num_vector)  # returns the mean of the elements in the vector
sum(num_vector)   # returns the sum of the elements in the vector
  • Logical Comparisons: Perform comparisons that return logical vectors. 
num_vector > 4   # returns c(FALSE, FALSE, TRUE, TRUE, TRUE, TRUE)

Practical Examples

Create and Manipulate a Vector of Scores: 

# Create the vector
scores <- c(12, 15, 14, 18, 16)
# Add a new score
scores <- c(scores, 17)
# Remove the lowest score
scores <- scores[scores != min(scores)]
# Calculate the average
mean(scores)  # returns the mean of the remaining scores

Use a Logical Vector to Filter Data: 

# Create a vector of numbers
numbers <- c(5, 8, 12, 15, 20)
# Create a logical vector to filter numbers greater than 10
filter_logical <- numbers > 10
# Use the logical vector to filter numbers
filtered_numbers <- numbers[filter_logical]  # returns c(12, 15, 20)

In summary, scalars and vectors are fundamental concepts in R. Scalars are vectors of length 1, while vectors can contain multiple elements of the same type. Understanding how to create, manipulate, and analyze these structures is essential for data analysis in R.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *