-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportingdata.R
More file actions
75 lines (48 loc) · 1.18 KB
/
Copy pathimportingdata.R
File metadata and controls
75 lines (48 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
---
title: "How to Import and Export Data in R"
author: "Vivian Guetler"
date: "5/28/2021"
---
# How to Import and Export Data in R
# Topics
# 1. Import data into R
# 2. Export data
# Data formats
# Excel files .xls or xlsx
# CSVs (comma separated values) .csv
# TSVs .tsv
# TXTs .txt
# First step: set up working directory
getwd()
#setwd("")
# Let's load our files into the environment
## Excel files
# Install packages
install.packages("readxl") # reads excels files into R
# Load the library
library(readxl)
# Read in the data file
df_excel <- read_excel("data.xlsx")
# View
View(df_excel)
head(df_excel)
# CSVs
#Install package
install.packages("readr")
# Load the library
library(readr)
# Read in CSV file
df_csv <- read_csv("data.csv")
## TSVs
df_tsv <- read_tsv("data.tsv")
## TXTs
df_txt <- read.table("data.txt", header = T)
### Export the files from R
# How to export to a .csv
# write_tsv()
# write.csv()
# write.table()
write_csv(df_csv, "my_new_csv_file.csv")
# Data for tutorial are from nycflights dataset
# An R data package containing all out-bound flights from NYC
# Link: <"https://raw.githubusercontent.com/jayleetx/nycflights/master/data-raw/planes.csv">