# Read with specific optionscars_data2 <-read.csv("sample_cars.csv", header =TRUE, # First row contains column namessep =",", # Separator is a commastringsAsFactors =FALSE, # Don't convert strings to factorsna.strings =c("NA", "N/A", "")) # Values to treat as NAhead(cars_data2)
Fixed-width files have fields of consistent width:
# Create a sample fixed-width filecat("John Smith 35\nMary Jones 28\nDavid Brown 42\n", file ="sample_people.txt")# Read the fixed-width filepeople_data <-read.fwf("sample_people.txt", widths =c(5, 6, 3), # Width of each columncol.names =c("First", "Last", "Age"))people_data
First Last Age
1 John Smith 35
2 Mary Jones 28
3 David Brown 42
Reading from R Data Files
R has its own binary file format for saving and loading R objects:
# Save R objects to a filesample_data <-list(x =1:10, y = letters[1:10])save(sample_data, file ="sample_data.RData")# Load the saved objectsload("sample_data.RData")sample_data
# Read CSV from a URL (example with a small dataset)url <-"https://raw.githubusercontent.com/datasets/iris/master/data/iris.csv"iris_data <-try(read.csv(url), silent =TRUE)# Check if the data was loaded successfullyif (!inherits(iris_data, "try-error")) {head(iris_data)} else {print("Could not access the URL. Check your internet connection.")}
[1] "Could not access the URL. Check your internet connection."
Reading Excel Files
While not part of base R, the readxl package is commonly used:
# Check if readxl is installedif (!requireNamespace("readxl", quietly =TRUE)) {message("The readxl package is not installed. You can install it with: install.packages('readxl')")} else {library(readxl)# This would read an Excel file if it existed# excel_data <- read_excel("sample.xlsx", sheet = 1)}
Reading from Databases
Base R provides the DBI package for database connections:
# Example of connecting to SQLite (not run)# if (!requireNamespace("RSQLite", quietly = TRUE)) {# message("The RSQLite package is not installed")# } else {# library(DBI)# con <- dbConnect(RSQLite::SQLite(), ":memory:")# dbWriteTable(con, "mtcars", mtcars)# data <- dbGetQuery(con, "SELECT * FROM mtcars WHERE cyl = 4")# dbDisconnect(con)# }
# List of files to removefiles_to_remove <-c("sample_cars.csv", "sample_cars.txt", "sample_people.txt", "sample_data.RData", "sample_cars.rds")# Remove filesfor (file in files_to_remove) {if (file.exists(file)) {file.remove(file) }}
Remember to check the documentation with ?read.csv or similar commands to explore all available options for these functions.