Data Import:: Cheatsheet R's tidyverse is built around tidy data stored in tibbles, which are enhanced data frames. The front side of this sheet shows how to read text files into R with readr. The reverse side shows how to create tibbles with tibble and to layout tidy data with tidyr. OTHERTYPESOFDATA Try one of the following packages to import other types of files • haven - SPSS, Stata, and SAS files readxl- excel files (.xls and .xlsx) DBI - databases • jsonlite-json • xml2-XML • httr-Web APIs • rvest-HTML (Web Scraping) Save Data Save x, an R object, to path, a file path, as: Comma delimited file write_csv(x, path, na = "NA", append = FALSE, col_names = lappend) File with arbitrary delimiter write_delim(x, path, delim = "", na = "NA", append = FALSE, col_names = lappend) CSV for excel write_excel_csv(x, path, na = "NA", append = FALSE, col_names = lappend) String to file write_file(x, path, append = FALSE) String vector to file, one element per line write_lines(x,path, na = "NA", append = FALSE) Object to RDS file write_rds(x, path, compress = c("none", "gz", "bz2", "xz"),...) Tab delimited files write_tsv(x, path, na = "NA", append = FALSE, col_names = lappend) ©Stud 10 Read Tabular Data These functions share the common arguments: read_*(file, col_names = TRUE, col_types = NULL, locale = default_locale(), na = c("", "NA"), quoted_na = TRUE, comment = "", trim_ws = TRUE, skip = 0, n_max = Inf, guess_max = min(1000, n_max), progress = interactiveQ) a,b,c 1,2,3 4,5,NA a;b;c 1;2;3 4;5;NA 1\2\3 4\5\NA K a b c 1 2 3 4 5 NA D 1 2 3 4 5 NA D 1 □ 2 B 3 4 5 NA □ 1 El 2 B 3 4 5 NA □ 1 El 2 B 3 4 5 NA Comma Delimited Files read_csv("file.csv") To make file.csv run: write_file(x = "a,b,c\nl,2,3\n4,5,NA", path = "file.csv") Semi-colon Delimited Files read_cs v2 (" f i I e2. cs v") write_file(x = "a;b;c\nl;2;3\n4;5;NA", path = "file2.csv") Files with Any Delimiter read_delim("file.txt", delim = "|") write_file(x = "a|b|c\nl|2|3\n4|5|NA", path = "file.txt") Fixed Width Files read_fwf("file.fwf", coLpositions = c(l, 3,5)) write_file(x = "a b c\nl 2 3\n4 5 NA", path = "file.fwf") Tab Delimited Files read_tsv("file.tsv") Also read_table(). write_file(x = Ma\tb\tc\nl\t2\t3\n4\t5\tNA", path = "file.tsv"; USEFULARGUMENTS l\ Example file a,b,c 1,2,3 4,5,NA A B C 1 2 3 4 5 NA Q A n B B c 1 2 3 4 5 NA write_file("a,b,c\nl,2,3\n4,5,NA","file.csv") f <- "file.csv" No header read_csv(f, col_names= FALSE) Provide header read_csv(f, col_names = c("x", "y", "z")) □ Skip lines 4 5 NA read_csv(f, skip = 1) □ □ 1 2 □ 3 Read in a subset read_csv(f, n_max = 1) DB NA 2 B 3 Missing Values read_csv(f,na = c("l",".")) 4 5 NA Read Non-Tabular Data Read a file into a single string read_file(file, locale = defaultJocale()) Read each line into its own string read_Iines(file, skip = 0, n_max = -1L, na = character), locale = default_locale(), progress = interactive()) Read Apache style log files read_Iog(file, col_names = FALSE, col_types = NULL, skip = 0, n_max = -1, progress = interactive()) RStudio® is a trademark of RStudio, Inc. • CCBYSA RStudio- info@rstudio.com • 844-448-1212 • rstudio.com • Lea rn more at tidyverse. Read a file into a raw vector read_file_raw(file) Read each line into a raw vector read_!ines_raw(file, skip = 0, n_max = -1L, progress = interactiveQ) Data types readrfunctions guess the types of each column and convert types when appropriate (but will NOT convert strings to factors automatically). A message shows the type of each column in the result. ## Parsed with column specification: ## cols( ## age = col_integer(), ## sex = col_character( ## earn = col_double() ## ) 1. Use problems)) to diagnose problems. x <- read_csv("file.csv"); problems(x) 2. Use a col_ function to guide parsing. • col_guess() the default • col_character() • col_double(), col_euro_double() • col_datetime(format = '") Also col_date(format=""), col_time(format= "") • col_factor(levels, ordered = FALSE) • col_integer() • col_logical() • col_number(), col_numeric() • col_skip() x<- read_csv("file.csv", col_types = cols( A = col_double(), B = col_logical(), C = colJactorQ)) 3. Else, read in as character vectors then parse with a parse_ function. • parse_guess() • parse_character() • parse_datetime() Also parse_date() and parse_time() • parse_double() • parse_factor() • parse_integer() • parse_logical() • parse_number() x$A <- parse_number(x$A) Eg • readr 1.1.0- tibble 1.2.12- tidyr 0.6.0- Updated:2019-oe Tibbies an enhanced data frame The tibble package provides a new S3 class for storing tabular data, the tibble. Tibbies inherit the data frame class, but improve three behaviors: • Subsetting- [ always returns a new tibble, [[ and $ always return a vector. • No partial matching - You must use full column names when subsetting • Display - When you print a tibble, R provides a concise view of the ^— data that fits on | # a tlbble: 234 „ 6 inufacturer model displ one screen audi a4 quattn audi a4 quattn audi a4 quattn It 1:8 a4 3.1 tibble display A large table to display 156 1999 6 157 1999 6 auto(l4) auto(l4) 158 2668 6 mim %* 161 1999 4 auto!14} auto(l4} ill V 166 1999 4 anuallmi! luíBltfl auto(l4} [ reached qe1 — omitted 68 Option! "max. print"} data frame display • Control the default appearance with options: options(tibble.print_max = n, tibble.print_min = m, tibble.width = Inf) • View full data set with View)) or glimpse)) • Revert to data frame with as.data.frame)) CONSTRUCT A TIBBLE IN TWO WAYS tibble)...) Construct by columns. tibble(x = l:3,y = c("a", "b", "c")j tribble)...) Construct by rows. tribble( ~x, ~y 1, "a", 2, "b", 3, "c") as_tibble(x,...) Convert data frame to tibble. enframefx, name = "name", value = "value") Convert named vector to a tibble is_tibble(x) Test whether x is a tibble. ©Stud 10 Tidy Data with tidyr Tidy data is a way to organize tabular data. It provides a consistent data structure across packages. A table is tidy if: Tidy data: & Each variable is in its own column Each observation, or case, is in its own row A * B -> C □ * Q Q Makes variables easy to access as vectors Preserves cases during vectorized operations Reshape Data Ch3 nge the layout of values in a table Use gather)) and spread)) to reorganize the values of a table into a new layout. spread(data, key, value, fill = NA, convert = FALSE, drop = TRUE, sep = NULL) spread() moves the unique values of a key column into the column names, spreading the values of a value column across the new columns. table2 gather(data, key, value,na.rm = FALSE, convert = FALSE, factor_key = FALSE) gather() moves column names into a key column, gathering the column values into a single value column. table4a Bwrmirmi-Miiii A 0.7k 2k B 37k 80k C 212k 213k A 11=1=1=1 0.7k b 1 IEEE 37k c 212k A aililil 2k b 80k 0 1 ailiiii 213k key value A 1999 E 3 0.7k _w 9 19m A 1999 0.7k 19m A 1999 U A 2000 2k 20m A 2000 B j 2k B 1999 37k 172m A 2000 K 9 20m B 2000 80k 174m B 1999 E HS 37k C 1999 212k 1t B 1999 M 9 172m C 2000 213k 1t B 2000 E B 80k B 2000 U 9 174m C 1999 E Hä 212k C 1999 K 9 1t C 2000 E !3S 213k C 2000 M 9 1t gather(table4a, '1999', '2000' key= "year", value = "cases") key valu* spread(table2, type, count) Handle Missing Values drop_na(data,...) Drop rows containing NA'sin ... columns. X D 3 E NA drop_na(x, x2) fill(data,.direction = cf'down", "up")) Fill in NA's in ... columns with most recent non-NA values. replace_na(data, replace = list(),...) Replace NA's by column. niia X X wswwta A 1 A 1 _W A 1 A 1 \ A 1 D 3 B NA B 1 B NA ~ B 2 C NA C 1 C C 2 D 3 D 3 D 3 D 3 E NA E 3 E NA E 2 fill(x, x2) replace_na(x, list(x2=2)) Expand Tables quickly create tables with combinations of values complete(data,fill = listQ) expand(data,...) Adds to the data missing combinations of the values of the variables listed in ... complete(mtcars, cyl, gear, carb) Create new tibble with all possible combinations of the values of the variables listed in ... expand(mtcars, cyl, gear, carb) Split Cells Use these functions to split or combine cells into individual, isolated values. separate(data, col, into, sep = "[A[:alnum:]] +", remove = TRUE, convert = FALSE, extra = "warn", fill = "warn",...) Separate each cell in a column to make several columns. table3 A 1999 0.7kai9m A 1999 0.7k B3H A 2000 2k/20m ^ A 2000 B 1999 37k/172m B 1999 2k Biffil 37k WBS B 2000 80k/174m B 2000 C 1999 212k/1t C 1999 C 2000 213k/1t C 2000 80k 1 separate(table3, rate, sep = into = c(''cases", "pop")) separate_rows(data, ...,sep= "[A[:alnum:].] +", convert = FALSE) Separate each cell in a column to make several rows. table3 A 1999 0.7k A 1999 1.7k/19m A 2000 2k/20m ->■ A 1999 mm B 1999 37kai72m A 2000 2k B 2000 80kai74m A 2000 C 1999 12kait B 1999 37k C 2000 13kait B 1999 B 2000 80k B 2000 C 1999 212k C 1999 ■EB C 2000 213k C 2000 ■m separate_rows(table3, rate, sep = "/") unite(data, col,sep = remove = TRUE) Collapse cells across several columns to make a single column. table5 Afghan 19 Afghan 1999 Afghan 20 -> Afghan 2000 Brazil 19 Brazil 1999 Brazil 20 Brazil 2000 China 19 China 1999 China 20 China 2000 unite(table5, century, year, col= "year", sep = "") RStudio8 isa trademark of RStudio, Inc. • CCBYSA RStudio- info@rstudio.com • 844-448-1212-rstudio.com- Learn more at tidwerse.org • readr 1.1.0- tibble 1.2.12- tidyr 0.6.0- Updated: 2019-'