-
Notifications
You must be signed in to change notification settings - Fork 125
/
ch-7.R
126 lines (73 loc) · 1.88 KB
/
ch-7.R
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# For importing and exploring data
library(tidyverse)
# For reading in Excel files
library(readxl)
# For summary statistics
library(psych)
# For writing data to Excel
library(writexl)
my_number <- 8.2
sqrt(my_number)
my_char <- 'Hello, world'
toupper(my_char)
is.vector(my_number)
length(my_number)
my_numbers <- c(5, 8, 2, 7)
is.vector(my_numbers)
str(my_numbers)
length(my_numbers)
sqrt(my_numbers)
roster_names <- c('Jack', 'Jill', 'Billy', 'Susie', 'Johnny')
toupper(roster_names)
my_vec <- c('A', 2, 'C')
my_vec
str(my_vec)
# Get third element of roster_names vector
roster_names[3]
# Get first through third elements
roster_names[1:3]
# Get second through last elements
roster_names[2:length(roster_names)]
# Get second and fifth elements
roster_names[c(2, 5)]
roster <- data.frame(
name = c('Jack', 'Jill', 'Billy', 'Susie', 'Johnny'),
height = c(72, 65, 68, 69, 66),
injured = c(FALSE, TRUE, FALSE, FALSE, TRUE))
roster
data()
head(iris)
is.data.frame(iris)
str(iris)
data(package = 'psych')
data('sat.act')
str(sat.act)
getwd()
file.exists('test-file.csv')
file.exists('test-folder/test-file.csv')
# This is only TRUE if you've placed the file one
# up from current folder location
file.exists('../test-file.csv')
read_csv('datasets/star/districts.csv')
districts <- read_csv("datasets/star/districts.csv")
districts
star <- read_xlsx("datasets/star/star.xlsx")
head(star)
View(star)
glimpse(star)
summary(star)
describe(star)
# Third row, second column of data frame
roster[3, 2]
# Second through fourth rows, first through third columns
roster[2:4, 1:3]
# Second and third rows only
roster[2:3,]
# First and third columns only
roster[, c(1,3)]
roster$height
is.vector(roster$height)
# Write roster data frame to csv
write_csv(roster, 'output/roster-output-r.csv')
# Write roster data frame to csv
write_xlsx(roster, 'output/roster-output-r.xlsx')