library(tidyverse)
tips2 <- read_csv("https://uoepsy.github.io/data/RestaurantTips2.csv")Read in and inspect data
To read in data
To read data from an online source into R, use the function read_csv() and give it the URL where the data is located. Here’s an example of reading in the data from https://uoepsy.github.io/data/RestaurantTips2.csv and storing it under the name tips2:
read_csv() comes from the tidyverse package, so you’ll need to load the tidyverse package if you haven’t already.
To inspect data
To look at the first few rows, use head():
head(tips2)# A tibble: 6 × 10
Bill Tip Credit Guests Day Server PctTip HadCoffee IQ1 IQ2
<dbl> <dbl> <chr> <dbl> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 23.7 10 n 2 f A 42.2 2 93 100
2 32.0 5.01 y 2 f A 15.7 2 96 98
3 17.4 3.61 y 2 f B 20.8 2 94 99
4 15.4 3 n 2 f B 19.5 2 99 108
5 18.6 2.5 n 2 f A 13.4 2 129 106
6 21.6 3.44 n 2 f B 16 2 82 118
tips2 |>
head()# A tibble: 6 × 10
Bill Tip Credit Guests Day Server PctTip HadCoffee IQ1 IQ2
<dbl> <dbl> <chr> <dbl> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 23.7 10 n 2 f A 42.2 2 93 100
2 32.0 5.01 y 2 f A 15.7 2 96 98
3 17.4 3.61 y 2 f B 20.8 2 94 99
4 15.4 3 n 2 f B 19.5 2 99 108
5 18.6 2.5 n 2 f A 13.4 2 129 106
6 21.6 3.44 n 2 f B 16 2 82 118
Note: Both versions of this code accomplish the same thing. One places tips2 inside the head() function, and the other one uses the pipe to feed tips2 into head().
To see how many rows and columns are in the data, use dim():
dim(tips2)[1] 99 10
tips2 |>
dim()[1] 99 10
To see how many rows are in the data, use nrow():
nrow(tips2)[1] 99
tips2 |>
nrow()[1] 99
To see how many columns are in the data, use ncol():
ncol(tips2)[1] 10
tips2 |>
ncol()[1] 10
To see the structure of the data, use str():
str(tips2)spc_tbl_ [99 × 10] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ Bill : num [1:99] 23.7 32 17.4 15.4 18.6 ...
$ Tip : num [1:99] 10 5.01 3.61 3 2.5 3.44 2.42 3 2 1.25 ...
$ Credit : chr [1:99] "n" "y" "y" "n" ...
$ Guests : num [1:99] 2 2 2 2 2 2 2 2 2 2 ...
$ Day : chr [1:99] "f" "f" "f" "f" ...
$ Server : chr [1:99] "A" "A" "B" "B" ...
$ PctTip : num [1:99] 42.2 15.7 20.8 19.5 13.4 16 12.4 12.7 10.7 14.2 ...
$ HadCoffee: num [1:99] 2 2 2 2 2 2 2 2 2 1 ...
$ IQ1 : num [1:99] 93 96 94 99 129 82 127 93 85 121 ...
$ IQ2 : num [1:99] 100 98 99 108 106 118 94 91 113 96 ...
- attr(*, "spec")=
.. cols(
.. Bill = col_double(),
.. Tip = col_double(),
.. Credit = col_character(),
.. Guests = col_double(),
.. Day = col_character(),
.. Server = col_character(),
.. PctTip = col_double(),
.. HadCoffee = col_double(),
.. IQ1 = col_double(),
.. IQ2 = col_double()
.. )
- attr(*, "problems")=<externalptr>
tips2 |>
str()spc_tbl_ [99 × 10] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ Bill : num [1:99] 23.7 32 17.4 15.4 18.6 ...
$ Tip : num [1:99] 10 5.01 3.61 3 2.5 3.44 2.42 3 2 1.25 ...
$ Credit : chr [1:99] "n" "y" "y" "n" ...
$ Guests : num [1:99] 2 2 2 2 2 2 2 2 2 2 ...
$ Day : chr [1:99] "f" "f" "f" "f" ...
$ Server : chr [1:99] "A" "A" "B" "B" ...
$ PctTip : num [1:99] 42.2 15.7 20.8 19.5 13.4 16 12.4 12.7 10.7 14.2 ...
$ HadCoffee: num [1:99] 2 2 2 2 2 2 2 2 2 1 ...
$ IQ1 : num [1:99] 93 96 94 99 129 82 127 93 85 121 ...
$ IQ2 : num [1:99] 100 98 99 108 106 118 94 91 113 96 ...
- attr(*, "spec")=
.. cols(
.. Bill = col_double(),
.. Tip = col_double(),
.. Credit = col_character(),
.. Guests = col_double(),
.. Day = col_character(),
.. Server = col_character(),
.. PctTip = col_double(),
.. HadCoffee = col_double(),
.. IQ1 = col_double(),
.. IQ2 = col_double()
.. )
- attr(*, "problems")=<externalptr>
To take a glimpse at the data types and the first few values of each column of the data, use glimpse():
glimpse(tips2)Rows: 99
Columns: 10
$ Bill <dbl> 23.70, 31.99, 17.39, 15.41, 18.62, 21.56, 19.58, 23.59, 18.6…
$ Tip <dbl> 10.00, 5.01, 3.61, 3.00, 2.50, 3.44, 2.42, 3.00, 2.00, 1.25,…
$ Credit <chr> "n", "y", "y", "n", "n", "n", "n", "n", "n", "n", "n", "n", …
$ Guests <dbl> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, …
$ Day <chr> "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", …
$ Server <chr> "A", "A", "B", "B", "A", "B", "A", "A", "B", "B", "B", "C", …
$ PctTip <dbl> 42.2, 15.7, 20.8, 19.5, 13.4, 16.0, 12.4, 12.7, 10.7, 14.2, …
$ HadCoffee <dbl> 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 0, 2, 2, 0, 1, 1, 2, 0, 2, …
$ IQ1 <dbl> 93, 96, 94, 99, 129, 82, 127, 93, 85, 121, 94, 76, 76, 98, 1…
$ IQ2 <dbl> 100, 98, 99, 108, 106, 118, 94, 91, 113, 96, 102, 84, 102, 9…
tips2 |>
glimpse()Rows: 99
Columns: 10
$ Bill <dbl> 23.70, 31.99, 17.39, 15.41, 18.62, 21.56, 19.58, 23.59, 18.6…
$ Tip <dbl> 10.00, 5.01, 3.61, 3.00, 2.50, 3.44, 2.42, 3.00, 2.00, 1.25,…
$ Credit <chr> "n", "y", "y", "n", "n", "n", "n", "n", "n", "n", "n", "n", …
$ Guests <dbl> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, …
$ Day <chr> "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", …
$ Server <chr> "A", "A", "B", "B", "A", "B", "A", "A", "B", "B", "B", "C", …
$ PctTip <dbl> 42.2, 15.7, 20.8, 19.5, 13.4, 16.0, 12.4, 12.7, 10.7, 14.2, …
$ HadCoffee <dbl> 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 0, 2, 2, 0, 1, 1, 2, 0, 2, …
$ IQ1 <dbl> 93, 96, 94, 99, 129, 82, 127, 93, 85, 121, 94, 76, 76, 98, 1…
$ IQ2 <dbl> 100, 98, 99, 108, 106, 118, 94, 91, 113, 96, 102, 84, 102, 9…