Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
119 views
in R by (141k points)
Can I apply a function to each element of a vector in R?

Please log in or register to answer this question.

1 Answer

0 votes
by (141k points)

Yes, you can apply a function to each element of a vector in R using various techniques. R provides several built-in functions and approaches to achieve this.

Using Vectorized Functions

R has many vectorized functions that are designed to operate on each element of a vector individually. These functions automatically apply the operation to each element and return a new vector as the result. Some common vectorized functions include sqrt(), log(), sin(), cos(), exp(), etc. 

Here's an example:

vector <- c(1, 2, 3, 4, 5)

# Applying square root to each element
result_sqrt <- sqrt(vector)
print(result_sqrt)  # Output: 1 1.414213 1.732051 2 2.236068

# Applying logarithm (base 10) to each element
result_log <- log10(vector)
print(result_log)  # Output: 0 0.30103 0.4771213 0.60206 0.69897
 

Using the sapply() Function

The sapply() function in R allows you to apply a function to each element of a vector and returns a simplified result. It applies the function to each element of the vector and returns a vector or matrix depending on the output of the function. 

Here's an example:

vector <- c(1, 2, 3, 4, 5)

# Applying a custom function to each element
result_custom <- sapply(vector, function(x) x^2)
print(result_custom)  # Output: 1 4 9 16 25
 

In the above example, the sapply() function applies the custom function (in this case, squaring the value) to each element of the vector and returns a new vector with the squared values.

Using a for Loop

You can also use a for loop to iterate over each element of the vector and apply a function. This approach is useful when you need more control over the iteration process. Here's an example:

vector <- c(1, 2, 3, 4, 5)
result <- numeric(length(vector))  # Create an empty vector to store the results

# Applying a function to each element using a for loop
for (i in 1:length(vector)) {
  result[i] <- vector[i]^2
}

print(result)  # Output: 1 4 9 16 25
 

In the above example, a for loop is used to iterate over each element of the vector, square the value, and store the result in a new vector.

These are a few ways you can apply a function to each element of a vector in R. The choice of method depends on the specific requirements and complexity of the function you want to apply.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jun 15, 2023 in R by kvdevika (141k points)
0 votes
1 answer
asked Jun 15, 2023 in R by kvdevika (141k points)
0 votes
1 answer
asked Jun 15, 2023 in R by kvdevika (141k points)
0 votes
1 answer
asked Jun 15, 2023 in R by kvdevika (141k points)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...