R base
functions
head
head
is a simple function that shows the first n
values in an object, with default n = 6
. Additionally, if you include a negative sign in front of your n
integer, it will display the object without the last n items.
Examples
Using the Iowa liquor sales file, which columns contain the Store Number, Store Name, Address, City, and Zip Code?
Click to see solution
library(data.table)
options(repr.matrix.max.cols=50)
iowa_1000 <- fread("/anvil/projects/tdm/data/iowa_liquor_sales/iowa_liquor_sales.csv", nrows=1000)
head(iowa_1000[, .(`Store Number`, `Store Name`, `Address`, `City`, `Zip Code`)])
Store Number Store Name Address City Zip Code 2846 CVS PHARMACY #8443 / CEDAR RAPIDS 3419 16TH AVE SW CEDAR RAPIDS 52404 3894 SMOKIN' JOE'S #6 TOBACCO AND LIQUOR 1404 1ST AVE NE CEDAR RAPIDS 52402 2558 HY-VEE FOOD STORE / MOUNT PLEASANT 1700 E WASHINGTON MOUNT PLEASANT 52641 4680 AFAL FOOD & LIQUOR / DES MOINES 4121 SE 14TH ST DES MOINES 50320 2590 HY-VEE FOOD STORE #5 / CEDAR RAPIDS 3235 OAKLAND ROAD NE CEDAR RAPIDS 52402 4126 SAM'S MAINSTREET MARKET / SOLON 123 E MAIN ST SOLON 52333
dim
dim
is a function that allows us to return or set the dimension of an object. We read the output/input of dim
as [rows, columns].
Examples
Using the Iowa liquor sales file, what is the dimension of the data set?
Click to see solution
library(data.table)
options(repr.matrix.max.cols=50)
iowa_full <- fread("/anvil/projects/tdm/data/iowa_liquor_sales/iowa_liquor_sales.csv", select=c("Store Number", "Store Name", "Address", "City", "Zip Code"))
dim(iowa_full)
27050143 5
table
table
is a function used to build a contingency table, which is a table that shows counts for categorical data, from one or more categories. prop.table
is a function that accepts table
output, returning proportions of the counts.
Examples
Using the Iowa liquor sales file, show a table with the 10 most popular values of the "Store Number" and the number of occurrences of each.
Click to see solution
library(data.table)
options(repr.matrix.max.cols=50)
iowa_full <- fread("/anvil/projects/tdm/data/iowa_liquor_sales/iowa_liquor_sales.csv", select=c("Store Number", "Store Name", "Address", "City", "Zip Code"))
store_num_freq <- table(iowa_full$`Store Number`)
head(sort(store_num_freq, decreasing=TRUE), 10)
2633 4829 2190 2512 2572 2603 2515 2614 2647 2648 223447 190702 173109 147581 142536 140319 133959 132915 132269 128608
Using the Iowa liquor sales file, show a table with the 10 most popular values of the "Store Name" and the number of occurrences of each.
Click to see solution
library(data.table)
options(repr.matrix.max.cols=50)
iowa_full <- fread("/anvil/projects/tdm/data/iowa_liquor_sales/iowa_liquor_sales.csv", select=c("Store Number", "Store Name", "Address", "City", "Zip Code"))
store_name_freq <- table(iowa_full$`Store Name`)
head(sort(store_name_freq, decreasing=TRUE), 10)
HY-VEE #3 / BDI / DES MOINES CENTRAL CITY 2 223447 190653 CENTRAL CITY LIQUOR, INC. HY-VEE FOOD STORE / CEDAR FALLS 173158 142536 HY-VEE WINE AND SPIRITS / IOWA CITY HY-VEE WINE AND SPIRITS / BETTENDORF 141557 140319 HY-VEE #7 / CEDAR RAPIDS HY-VEE #4 / WDM 131294 128608 HY-VEE FOOD STORE #1 / MASON CITY BENZ DISTRIBUTING 127264 120357
Using the Iowa liquor sales file, show a table with the 10 most popular values of these three columns pasted together: Address, City, "Zip Code" and the number of occurrences of each.
Click to see solution
library(data.table)
options(repr.matrix.max.cols=50)
iowa_full <- fread("/anvil/projects/tdm/data/iowa_liquor_sales/iowa_liquor_sales.csv", select=c("Store Number", "Store Name", "Address", "City", "Zip Code"))
store_location_freq <- table(paste(iowa_full$Address, iowa_full$City, iowa_full$`Zip Code`, sep="| "))
head(sort(store_location_freq, decreasing=TRUE), 10)
3221 SE 14TH ST| DES MOINES| 50320 223447 1501 MICHIGAN AVE| DES MOINES| 50314 190702 1460 2ND AVE| DES MOINES| 50314 173109 1720 WATERFRONT DR| IOWA CITY| 52240 147581 6301 UNIVERSITY| CEDAR FALLS| 50613 142536 2890 DEVILS GLEN ROAD| BETTENDORF| 52722 140319 2400 4TH ST SW| MASON CITY| 50401 133959 1823 E KIMBERLY RD| DAVENPORT| 52807 132915 5050 EDGEWOOD RD| CEDAR RAPIDS| 52411 132269 555 S 51ST ST| WEST DES MOINES| 50265 128608