R How to Write a List to a TXT or CSV File (2 Examples)
In this tutorial you’ll learn how to export a list as CSV or TXT in R programming.
Example Data
example_list <- list("XYZ", # Constructing example list 100:95, 1:5, letters[3:10], c("x", "LALA", "abc")) example_list # Showing example list in RStudio console # [[1]] # [1] "XYZ" # # [[2]] # [1] 100 99 98 97 96 95 # # [[3]] # [1] 1 2 3 4 5 # # [[4]] # [1] "c" "d" "e" "f" "g" "h" "i" "j" # # [[5]] # [1] "x" "LALA" "abc" # |
example_list <- list("XYZ", # Constructing example list 100:95, 1:5, letters[3:10], c("x", "LALA", "abc")) example_list # Showing example list in RStudio console # [[1]] # [1] "XYZ" # # [[2]] # [1] 100 99 98 97 96 95 # # [[3]] # [1] 1 2 3 4 5 # # [[4]] # [1] "c" "d" "e" "f" "g" "h" "i" "j" # # [[5]] # [1] "x" "LALA" "abc" #
Example 1: Write List to TXT File Using capture.output() Function
capture.output(example_list, # Export as TXT file file = "example_list.txt") |
capture.output(example_list, # Export as TXT file file = "example_list.txt")
Example 2: Write List to CSV File Using capture.output() Function
capture.output(example_list, # Export as CSV file file = "example_list.csv") |
capture.output(example_list, # Export as CSV file file = "example_list.csv")