How to Split a List in R


How to Split a List in R ?

Answer

To split a list in R, you can use the split function or create sublists manually using list manipulation.



✐ Examples

1 Split a List Using split

In this example,

  1. We create a vector named numbers with initial integer values.
  2. We use the split function to split the vector into groups based on a factor.
  3. The split function returns a list, where each element is a subgroup of the original vector.
  4. Finally, we print the list of subgroups to standard output.

R Program

numbers <- c(1, 2, 3, 4, 5, 6, 7, 8)
subgroups <- split(numbers, factor(rep(1:3, each = 3)))
print(subgroups)

Output

$`1`
[1] 1 2 3

$`2`
[1] 4 5 6

$`3`
[1] 7 8

2 Split a List into Chunks Manually

In this example,

  1. We create a vector named numbers with initial integer values.
  2. We define a chunk size.
  3. We use a for loop to iterate over the vector and create sublists of the specified chunk size.
  4. The sublists are stored in a list, where each element represents a chunk of the original vector.
  5. Finally, we print the list of chunks to standard output.

R Program

numbers <- c(1, 2, 3, 4, 5, 6, 7, 8)
chunk_size <- 3
chunks <- list()
for (i in seq(1, length(numbers), by = chunk_size)) {
  chunks[[length(chunks) + 1]] <- numbers[i:min(i + chunk_size - 1, length(numbers))]
}
print(chunks)

Output

[[1]]
[1] 1 2 3

[[2]]
[1] 4 5 6

[[3]]
[1] 7 8

Summary

In this tutorial, we learned How to Split a List in R language with well detailed examples.




More R Lists Tutorials

  1. How to create an Empty List in R ?
  2. How to Initialize a List in R ?
  3. How to Get Length of a List in R ?
  4. How to create a List of size N in R ?
  5. How to create a List of Numbers from 1 to N in R ?
  6. How to create a List of Strings in R ?
  7. How to create a List of Empty Lists in R ?
  8. How to Access Elements in a List in R ?
  9. How to get Element in a List at a Specific Index in R ?
  10. How to get First Element in a List in R ?
  11. How to get Last Element in a List in R ?
  12. How to Iterate Over a List in R ?
  13. How to Iterate Over a List with Index in R ?
  14. How to Iterate Over a List in Reverse Order in R ?
  15. How to check if a List is Empty in R ?
  16. How to check if a List is Not Empty in R ?
  17. How to get Sub List in R ?
  18. How to get the Index of Specified Element in a List in R ?
  19. How to check if a Specific Element is present in the List in R ?
  20. How to check if a List contains all the elements of Another List in R ?
  21. How to count the Number of Occurrences of Specific Element in the List in R ?
  22. How to find the Element with Maximum Number of Occurrences in a List in R ?
  23. How to find the Element with Minimum Number of Occurrences in a List in R ?
  24. How to Sort a List in R ?
  25. How to Sort a List in Ascending Order in R ?
  26. How to Sort a List in Descending Order in R ?
  27. How to create a Two Dimensional List in R ?
  28. How to Iterate over a Two Dimensional List in R ?
  29. How to create a Three Dimensional List in R ?
  30. How to Copy a List in R ?
  31. How to deep Copy a List in R ?
  32. How to Split a List in R ?
  33. How to Join Lists in R ?
  34. How to Append an Element to a List in R ?
  35. How to Insert an Element at Specific Index in a List in R ?
  36. How to Append a List to another List in R ?
  37. How to Concatenate Two Lists in R ?
  38. How to check if Two Lists are Equal in R ?
  39. How to check if Two Lists have Same Elements (Regardless of Order) in R ?
  40. How to Convert a List of Integers to a List of Strings in R ?
  41. How to Convert a List of Strings to a List of Integers in R ?
  42. How to Convert a List of Floats to a List of Strings in R ?
  43. How to Convert a List of Strings to a List of Floats in R ?
  44. How to Reverse a List in R ?
  45. How to Slice a List in R ?
  46. How to Slice First N Elements from a List in R ?
  47. How to Slice Last N Elements from a List in R ?
  48. How to Rotate Elements in a List in R ?
  49. How to Filter Elements of a List based on a Condition in R ?
  50. How to Remove Duplicates in a List in R ?
  51. How to Remove Element at a Specific Index from a List in R ?
  52. How to Remove Specific Element from a List in R ?
  53. How to Remove Element from List based on a Condition in R ?
  54. How to Sort a List of Strings in Dictionary Order in R ?
  55. How to Concatenate Strings in List in R ?
  56. How to create a List of Lists in R ?
  57. How to create a List of Dictionaries in R ?
  58. How to create a List of Sets in R ?