0% found this document useful (0 votes)
5 views1 page

Working With Data From Files in R

The document provides examples of how to read data from files in R using various functions from the readr library. It includes methods for reading text files, tab-separated files, and CSV files, demonstrating the use of functions like read.delim(), read_lines(), read_file(), and read_csv(). Each example prints the data read from the specified files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Working With Data From Files in R

The document provides examples of how to read data from files in R using various functions from the readr library. It includes methods for reading text files, tab-separated files, and CSV files, demonstrating the use of functions like read.delim(), read_lines(), read_file(), and read_csv(). Each example prints the data read from the specified files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Working with Data from Files in R

Simple R Program (File Reading Examples)

# Load required library


library(readr)

# 1. Read a text file using read.delim()


data1 <- read.delim("data.txt", header = FALSE)
cat("1. Data using read.delim():\n")
print(data1)

# 2. Read a tab-separated text file using read.delim2()


data2 <- read.delim2("data.txt", header = FALSE)
cat("\n2. Data using read.delim2():\n")
print(data2)

# 3. Read first line using read_lines()


line1 <- read_lines("data.txt", n_max = 1)
cat("\n3. First line using read_lines():\n")
print(line1)

# 4. Read two lines using read_lines()


line2 <- read_lines("data.txt", n_max = 2)
cat("\n4. First two lines using read_lines():\n")
print(line2)

# 5. Read whole file using read_file()


whole_file <- read_file("data.txt")
cat("\n5. Entire file using read_file():\n")
print(whole_file)

# 6. Read CSV file using read_csv()


csv1 <- read_csv("data.csv")
cat("\n6. CSV file using read_csv():\n")
print(csv1)

# 7. Read CSV using read.csv()


csv2 <- read.csv("data.csv", header = TRUE)
cat("\n7. CSV using read.csv():\n")
print(csv2)

# 8. Read CSV using read.table()


csv3 <- read.table("data.csv", header = TRUE, sep = ",")
cat("\n8. CSV using read.table():\n")
print(csv3)

You might also like