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