# If you add an extension to the file name, write_()* will automatically compress the output. write_csv(metadata, 'metadata.csv') write_tsv(metadata, 'metadata2.csv') write_tsv(metadata, 'metadata.tsv.gz')
library("xlsx") # Write the first data set in a new workbook write.xlsx(USArrests, file = "myworkbook.xlsx", sheetName = "USA-ARRESTS", append = FALSE) # Add a second data set in a new worksheet write.xlsx(mtcars, file = "myworkbook.xlsx", sheetName="MTCARS", append=TRUE) # Add a third data set write.xlsx(iris, file = "myworkbook.xlsx", sheetName="IRIS", append=TRUE)
2 tidyr包:整理数据
2-1 tidyr包提供了几个新函数,能够有效整理数据
gather(): makes “wide” data longer
spread(): makes “long” data wider
separate(): splits a single column into multiple columns
unite(): combines multiple columns into a single column
mutate() adds new variables that are functions of existing variables.
summarise() reduces multiple values down to a single summary.
Rows:
filter()chooses rows based on column values.
arrange()changes the order of the rows.
slice()chooses rows based on location.
Columns:
select()changes whether or not a column is included.
rename()changes the name of columns.
mutate()changes the values of columns and creates new columns.
relocate()changes the order of the columns.
Groups of rows:
summarise()collapses a group into a single row.
library(dplyr) head(starwars) ## # A tibble: 6 x 14 ## name height mass hair_color skin_color eye_color birth_year sex gender ## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> ## 1 Luke Sk~ 172 77 blond fair blue 19 male mascu~ ## 2 C-3PO 167 75 <NA> gold yellow 112 none mascu~ ## 3 R2-D2 96 32 <NA> white, bl~ red 33 none mascu~ ## 4 Darth V~ 202 136 none white yellow 41.9 male mascu~ ## 5 Leia Or~ 150 49 brown light brown 19 fema~ femin~ ## 6 Owen La~ 178 120 brown, grey light blue 52 male mascu~ ## # ... with 5 more variables: homeworld <chr>, species <chr>, films <list>, ## # vehicles <list>, starships <list>
starwars %>% dplyr::select(name, ends_with("color")) ## # A tibble: 87 x 4 ## name hair_color skin_color eye_color ## <chr> <chr> <chr> <chr> ## 1 Luke Skywalker blond fair blue ## 2 C-3PO <NA> gold yellow ## 3 R2-D2 <NA> white, blue red ## 4 Darth Vader none white yellow ## 5 Leia Organa brown light brown ## 6 Owen Lars brown, grey light blue ## 7 Beru Whitesun lars brown light blue ## 8 R5-D4 <NA> white, red red ## 9 Biggs Darklighter black light brown ## 10 Obi-Wan Kenobi auburn, white fair blue-gray ## # ... with 77 more rows
starwars %>% mutate(name, bmi = mass / ((height / 100) ^ 2)) %>% dplyr::filter(species == "Droid") %>% dplyr::select(name:mass, bmi) %>% arrange(desc(mass)) ## # A tibble: 6 x 4 ## name height mass bmi ## <chr> <int> <dbl> <dbl> ## 1 IG-88 200 140 35 ## 2 C-3PO 167 75 26.9 ## 3 R2-D2 96 32 34.7 ## 4 R5-D4 97 32 34.0 ## 5 R4-P17 96 NA NA ## 6 BB8 NA NA NA
starwars %>% group_by(species) %>% summarise(n = n(), mass = mean(mass, na.rm = TRUE)) %>% filter(n > 1, mass > 50) ## # A tibble: 8 x 3 ## species n mass ## <chr> <int> <dbl> ## 1 Droid 6 69.8 ## 2 Gungan 3 74 ## 3 Human 35 82.8 ## 4 Kaminoan 2 88 ## 5 Mirialan 2 53.1 ## 6 Twi'lek 2 55 ## 7 Wookiee 2 124 ## 8 Zabrak 2 80
4 purrr包:函数式编程
用R写循环从低到高有三种境界:手动 for 循环,apply 函数族,purrr 包泛函式编程。
map(.x, .f, …): Apply a function to each element of a list or vector. map(x, is.logical)
map2(.x, .y, .f, …): Apply a function to pairs of elements from two lists, vectors. map2(x, y, sum)
pmap(.l, .f, …): Apply a function to groups of elements from list of lists, vectors. pmap(list(x, y, z), sum, na.rm = TRUE)
Writing Data From R to Excel Files (xls|xlsx): http://www./english/wiki/writing-data-from-r-to-excel-files-xls-xlsx#writing-excel-files-using-xlsx-package
[3]
Reshaping Your Dat with tidyr: https://uc-r./tidyr