Thursday 8 June 2017

R Useful Functions

mtcars <- transform(mtcars, mpg = mpg ^ 2)

col(matrix/data.frame) or row(matrix/data.frame): to indicate row/col index of a matrix or a data.frame

file.info(dout)$isdir: boolean variable indicating whether the directory exist.

I(): A copy of the object with class "AsIs" prepended to the class(es).

gl() function generates factors by specifying the pattern of their levels.

interaction() computes a factor which represents the interaction of the given factors. The result of interaction is always unordered.

dput() and dget()

dplyr::near(float.num1,float.num2): to compare equality of two floating numbers.

dplyr::select_(data, "year", "month", "day")
col_vector <- c("year", "month", "day")
dplyr::select_(data, .dots = col_vector)
select_(data, 'year:day')
select_(data, 'year:day', '-month')
select_(data, '-(year:day)')
select_(data, 'starts_with("arr")')
select_(data, '-ends_with("time")')
select_(data, .dots = c('starts_with("arr")', '-ends_with("time")'))

ggplot2::cut_width: makes groups of width ‘width’
ggplot2::cut_interval: makes ‘n’ groups with equal range
ggplot2::cut_number: makes ‘n’ groups with (approximately) equal numbers of observations

geom_freqpoly(): instead of geom_histogram(). geom_freqpoly() performs the same calculation as geom_histogram(), but instead of displaying the counts with bars, uses lines instead.

dplyr::between(vector, left, right): a shortcut for ‘x >= left & x <= right’

stats::reorder(): treats its first argument as a categorical variable, and reorders its levels based on the values of a second variable, usually numeric.

relevel(): is a special case of simply calling factor(x, levels = levels(x)[....])

dplyr::count() is a short-hand for group_by() + tally()

geom_count
geom_tile

geom_bin2d() and geom_hex() divide the coordinate plane into 2d bins and then use a fill color to display how many points fall into each bin. geom_bin2d() creates rectangular bins. geom_hex()creates hexagonal bins.

modelr: modelling functions that work with the pipe.

as_tibble: to convert data.frame to tibble. tibble() does much less: it never changes the type of the inputs (e.g. it never converts strings to factors!), it never changes the names of variables, and it never creates row names.

tribble(), short for transposed tibble. tribble() is customised for data entry in code: column headings are defined by formulas (i.e. they start with ~).

lubridate::now()
lubridate::today()

With tibble, you can explicitly print() the data frame and control the number of rows (n) and the widthof the display. width = Inf will display all columns.
  • options(tibble.print_max = n, tibble.print_min = m): if more than m rows, print only nrows. Use options(dplyr.print_min = Inf) to always show all rows.
  • Use options(tibble.width = Inf) to always print all columns, regardless of the width of the screen.
You can browse at the package help with package?xml2.

parse_logical
parse_integer
parse_date

charToRaw("Hadley") #> [1] 48 61 64 6c 65 79
Each hexadecimal number represents a byte of information: 48 is H, 61 is a, and so on.

jsonlite::fromJSON
jsonlite:toJSON

gather() converts data from wide format to long format.
spread() converts data from long format to wide format.

tidyr::separate() pulls apart one column into multiple columns, by splitting wherever a separator character appears.

tidyr::unite() takes a data frame, the name of the new variable to create, and a set of columns to combine.

tidyr::complete() Turns implicit missing values into explicit missing values. This is a wrapper around ‘expand()’, ‘left_join()’ and ‘replace_na’ that's useful for completing missing combinations of data. It takes a set of columns, and finds all unique combinations. It then ensures the original dataset contains all those values, filling in explicit NAs where necessary.

tidyr::fill(). It takes a set of columns where you want missing values to be replaced by the most recent non-missing value (sometimes called last observation carried forward).

writeLines() to see the raw content of a string.

?"'" to see the complete list of special characters.


No comments:

Post a Comment