Learn key R functions Explained: like sort(), search(), subset(), sample(), all(), and any() with practical examples. Discover how to check if an element exists in a vector and understand the differences between all() and any(). Perfect for R beginners!” learn Q&A guide on sort(), search(), subset(), sample(), all(), any(), and element checks in vectors. Boost your R skills today!”
Table of Contents
Which function is used for sorting in the R Language?
Several functions in R can be used for sorting data. The most commonly used R functions for sorting are:
sort(): Sorts a vector in ascending or descending order. The general syntax issort(x, decreasing = FALSE, na.last = NA)order(): Returns the indices that would sort a vector (it is useful for sorting data frames). The general syntax oforder()is order(x, decreasing = FALSE, na.last = TRUE)arrange(): It sorts a data frame (however, it requiresdplyrpackage). The general syntax is: arrange(.data, …, .by_group = FALSE)
# sort() Function
vec <- c(3, 1, 4, 1, 5)
sort(vec) # Ascending (default): 1 1 3 4 5
sort(vec, decreasing = TRUE) # Descending: 5 4 3 1 1
# order() Function
df <- data.frame(name = c("Ali", "Usman", "Umar"), age = c(25, 20, 30))
df[order(df$age), ] # Sort data frame by age (ascending)
# arrange() Function from dplyr package
library(dplyr)
df %>% arrange(age) # Ascending
df %>% arrange(desc(age)) # Descending
Why search() function used?
In the R language, the search() function is used to display the current search path of R objects (such as functions, datasets, variables, etc.). This shows the order in which R looks for objects when you reference them.
What does search() function do?
- Lists all attached packages and environments in the order R searches them.
- Helps diagnose issues when multiple packages have functions with the same name (name conflicts).
- Shows where R will look when you call a function or variable.
What is the use of subset() and sample() functions in R?
In the R language, subset() and sample() are two useful functions for data manipulation and sampling:
subset(): is used to extract subsets of data frames or vectors based on some condition. The general syntax is subset(x, subset, select, …)sample(): is used for random sampling from a dataset with or without replacement. The general system is: sample(x, size, replace = FALSE, prob = NULL).
The examples of subset() and sample() are describe below
# Example data frame
df <- data.frame(
name = c("Ali", "Usman", "Aziz", "Daood"),
age = c(25, 30, 22, 28),
salary = c(50000, 60000, 45000, 70000)
)
# Filter rows where age > 25
subset(df, age > 25)
# Filter rows and select specific columns
subset(df, salary > 50000, select = c(name, salary))
# Randomly sample 3 numbers from 1 to 10 without replacement sample(1:10, 3) # Sample with replacement (possible duplicates) sample(1:5, 10, replace = TRUE) # Sample rows from a data frame df[sample(nrow(df), 2), ] # Picks 2 random rows

What is the use of all() and any()?
In the R language, the all() and any() functions are logical functions used to evaluate conditions across vectors or arrays.
all()function: checks if all elements of a logical vector areTRUE. It returnsTRUEonly if every element in the input isTRUE, otherwise, it returnsFALSE. The general syntax isall(..., na.rm=FALSE)any()Function: checks if at least one element of a logical vector isTRUE. It returnsTRUEif any element isTRUEandFALSEonly if all areFALSE. The general syntax isany(..., na.rm = FALSE)
The examples of all() and any() functions are:
x <- c(TRUE, TRUE, FALSE) all(x) # FALSE (not all elements are TRUE) y <- c(5, 10, 15) all(y > 3) # TRUE (all elements are greater than 3)
x <- c(TRUE, FALSE, FALSE) any(x) # TRUE (at least one element is TRUE) y <- c(2, 4, 6) any(y > 5) # TRUE (6 is greater than 5)
Note that if NA is present and na.rm = FALSE, any() returns NA unless a TRUE value exists.
What are the key differences between all() and any()?
The key differences between all() and any() are:
| Function | Returns TRUE When | Returns FALSE When |
|---|---|---|
all() | All elements are TRUE | At least one is FALSE |
any() | At least one element is TRUE | All are FALSE |
What is the R command to check if element 15 is present in a vector $x$?
One can check if the element (say) 15 is present in a vector x using either
%in%Operatorany()with logical comparisonwhich()to find the position of 15
# %in% x <- c(10, 15, 20, 25) 15 %in% x # Returns TRUE 30 %in% x # Returns FALSE # any() x <- c(5, 10, 15) any(x == 15) # TRUE any(x == 99) # FALSE # Which() x <- c(10, 15, 20, 15) which(x == 15) # Returns c(2, 4)


