In R, you can split a vector into smaller parts using the split() function.
In this example,
data
containing elements with corresponding factor levels.split()
function to split the vector into smaller parts based on the factor levels.data <- c(1, 2, 3, 4, 5, 6)
factor <- c('A', 'B', 'A', 'B', 'C', 'C')
split_data <- split(data, factor)
print(split_data)
{"A":[1,3],"B":[2,4],"C":[5,6]}
In this example,
data
containing numeric values.split()
function along with the condition to split the vector into two parts based on the condition.data <- c(1, 2, 3, 4, 5, 6)
condition <- data > 3
split_data <- split(data, condition)
print(split_data)
{"FALSE":[1,2,3],"TRUE":[4,5,6]}
In this tutorial, we learned How to Split a Vector in R language with well detailed examples.