Efficient Data Import Techniques in R

importing data in r importing data in r n.w
1 / 17
Embed
Share

Learn how to import data efficiently in R using functions like read.csv, read.delim, read.table, and readxl. Explore best practices for managing datasets with example code snippets and visual guides.

  • R programming
  • Data import
  • Data management
  • Data analysis
  • Data processing

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Importing Data in R Importing Data in R

  2. Outline read.csv read.delim read.table library("readr") library("data.table") library("readxl")

  3. read.csv # Import swimming_pools.csv: pools pools <- read.csv("swimming_pools.csv") # Print the structure of pools str(pools)

  4. read.csv # Import swimming_pools.csv correctly: pools pools <- read.csv("swimming_pools.csv", stringsAsFactors = FALSE) # Check the structure of pools str(pools)

  5. read.delim # Import hotdogs.txt: hotdogs hotdogs <- read.delim("hotdogs.txt", header = FALSE) # Summarize hotdogs summary(hotdogs)

  6. read.table # Import the hotdogs.txt file: hotdogs hotdogs <- read.table("hotdogs.txt", sep = "\t", col.names = c("type", "calories", "sodium")) # Call head() on hotdogs head(hotdogs)

  7. Exercise # Finish the read.delim() call hotdogs <- read.delim("hotdogs.txt", header = FALSE, col.names = c("type", "calories", "sodium")) # Select the hot dog with the least calories: lily lily <- hotdogs[which.min(hotdogs$calories), ] # Select the observation with the most sodium: tom tom <- hotdogs[which.max(hotdogs$sodium), ] # Print lily and tom lily tom

  8. library("readr")

  9. read_csv # Load the readr package install.packages("readr") library("readr") # Import potatoes.csv with read_csv(): potatoes potatoes <- read_csv("potatoes.csv")

  10. read_tsv library("readr") # Column names properties <- c("area", "temp", "size", "storage", "method", "texture", "flavor", "moistness") # Import potatoes.txt: potatoes potatoes <- read_tsv("potatoes.txt", col_names = properties) # Call head() on potatoes head(potatoes)

  11. read_delim library("readr") properties <- c("area", "temp", "size", "storage", "method", "texture", "flavor", "moistness") # Import potatoes.txt using read_delim(): potatoes potatoes <- read_delim("potatoes.txt", delim = "\t", col_names = properties) # Print out potatoes potatoes

  12. skip:the number of lines you're ignoring n_max: the number of lines you're actually importing. skip and n_max library("readr") # Column names properties <- c("area", "temp", "size", "storage", "method", "texture", "flavor", "moistness") potatoes_fragment <- read_tsv("potatoes.txt", skip = 6, n_max = 5, col_names = properties) potatoes_fragment

  13. library("data.table") library("readxl")

  14. library(data.table) potatoes <- fread("potatoes.csv", select = c(6, 8)) plot(potatoes$texture, potatoes$moistness)

  15. # Load the readxl package library(readxl) # Print out the names of both spreadsheets excel_sheets("urbanpop.xlsx")

  16. library(readxl) pop_1 <- read_excel("urbanpop.xlsx", sheet = 1) pop_2 <- read_excel("urbanpop.xlsx", sheet = 2) pop_3 <- read_excel("urbanpop.xlsx", sheet = 3) pop_list <- list(pop_1, pop_2, pop_3) pop_list

  17. paste("Hello", "World", sep = "-") paste0("Hello", "World") library(readxl) # Import Excel sheet of urbanpop_nonames.xlsx (R gives names): pop_a pop_a <- read_excel("urbanpop_nonames.xlsx", col_names = FALSE) # Import Excel sheet of urbanpop_nonames.xlsx (specify col_names): pop_b cols <- c("country", paste0("year_", 1960:1966)) pop_b <- read_excel("urbanpop_nonames.xlsx", col_names = cols) head(pop_a) head(pop_b)

More Related Content