Wednesday, April 23, 2014

R commands

See also why use R and the RSS feed of posts labelled R.

R code in this post is garbage this shows the limits of blogger for displaying R code which contains the assignment operator <- .="" a="" href="http://www.r-bloggers.com/three-ways-to-format-r-code-for-blogger/" is="" one="" solution="" to="">paste html from knitr documents
.


R mailing list: Use < - data-blogger-escaped-assignment="" data-blogger-escaped-comment----="" data-blogger-escaped-for="" data-blogger-escaped-functions="">

Set operations


x = letters[1:3]
y = letters[3:5]
union(x, y)
## [1] "a" "b" "c" "d" "e"
intersect(x, y)
## [1] "c"
setdiff(x, y)
## [1] "a" "b"
setdiff(y, x)
## [1] "d" "e"
setequal(x, y)
## [1] FALSE

Information about your R system

sessionInfo()
installed.packages()

Handling files

getwd()
list.files(tempdir()) 
dir.create("blabla")
read.csv("data.csv")

Lists

Given a list structure x, unlist simplifies it to produce a vector which contains all the atomic components which occur in x.
l1 <- a="a" b="2," c="pi+2i)" font="" list="" nbsp="">
unlist(l1) # a character vector
x<- 1="" br="">
x<-1

S3 methods

x<-1



List all available methods for a class:

methods(class="lm")

 One liners

Remove all objects in the workspace except one :

rm(list=ls()[!ls()=="object_to_keep"])

knitr

Those 2 commands are different.
Sets the options for chunk, within a knitr chunk inside the .Rmd document

opts_chunk$set(fig.width=10)
 Sets the options for knitr outside the .Rmd document

opts_knit$set()

dplyr

pipes
cars %>%
  group_by(speed) %>%
  print %>%
  summarise(numberofcars = n(),
            min = min(dist),
            mean = mean(dist),
            max = max(dist))

group_by() creates a tbl_df objects which is a wrapper around a data.frame to enable some functionalities. Note that print returns its output on a tbl_df object. So print() can be used inside the pipe without stopping the workflow.


 plyr (I replaced it with dplyr)

progress bar

l_ply(1:100000, identity, .progress = "win")
Rename items in a dataframe with revalue

sawnwood$item <- br="" item="" revalue="" sawnwood="">    c("Sawnwood (C)" = "Sawnwood Coniferous",
   "Sawnwood (NC)" = "Sawnwood Non Coniferous"))
Rename column names by their names



rename(mtcars, c("disp" = "displacement"))

Plotting with ggplot2


No comments: