Data Import and Export in R
By Kelvin Kiprono
September 4, 2024
Importing Data
CSV Files:
- Import a CSV file
data <- read.csv(“path/to/your/file.csv”) print(head(data)) # Display the first few rows of the data
Excel Files (using the readxl package):
- Install and load the readxl package
install.packages(“readxl”) library(readxl)
- Import an Excel file
data <- read_excel(“path/to/your/file.xlsx”, sheet = 1) print(head(data))
Text Files:
-
Import a tab-delimited text file data <- read.table(“path/to/your/file.txt”, header = TRUE, sep = “\t”) print(head(data))
- Other Formats: R has packages like haven for SPSS, SAS, and Stata files and jsonlite for JSON data.
Exporting Data
CSV Files:
- Export a data frame to a CSV file
write.csv(data, “path/to/your/output.csv”, row.names = FALSE) Excel Files (using the writexl package):
-
Install and load the writexl package install.packages(“writexl”) library(writexl)
-
Export a data frame to an Excel file write_xlsx(data, “path/to/your/output.xlsx”) Text Files:
-
Export a data frame to a tab-delimited text file write.table(data, “path/to/your/output.txt”, sep = “\t”, row.names = FALSE)