To slice the first N elements from a list in R, you can use index-based subsetting.
In this example,
numbers
with integer values.numbers[1:n]
to slice the first N elements.numbers <- c(1, 2, 3, 4, 5)
n <- 3
sliced_numbers <- numbers[1:n]
cat('Sliced list:', sliced_numbers)
Sliced list: 1 2 3
In this example,
words
with string values.words[1:n]
to slice the first N elements.words <- c('apple', 'banana', 'cherry', 'date', 'elderberry')
n <- 2
sliced_words <- words[1:n]
cat('Sliced list:', sliced_words)
Sliced list: apple banana
In this tutorial, we learned How to Slice First N Elements from a List in R language with well detailed examples.