R - ilya-khadykin/notes-outdated GitHub Wiki

TO DO:

Features:

  • operators have contextual behaviour;
  • most of operations on vectors (arrays) doesn't require writing loops;

Style guide: Google's R Style Guide

Syntax

Operators

operator comment
<- assignment
1:10 returns a list of numbers between 1 and 10
x <- 1:5 # assignment operator
y <- c(5,6,7,8,9,10)

Comments

# single line comment

Functions

print("Hello World!")
function result
help("library") shows help for a particular function
x <- c(1,2,3,4) concatenate elements
ls() list objects
str(test.csv) show structure of an object
install.packages("devtools") install packages
library(AnomalyDetection) library and require load and attach add-on packages.

Read CSV file

test.csv <- read.csv("C:\\Users\\ikhadykin\\Desktop\\test.csv", header = TRUE, sep = ",") # Windows
d <- read.csv("C:\\Users\\Ilya\\Documents\\R\\subset_rockstar_playercount_xboxonemp_metrics.csv", header = T, sep = ";")

Data type conversions

Convert data from R's factor to string in a data frame

# convert data from R's factor to string in a data frame
f <- sapply(d, is.factor)
d[f] <- lapply(d[f], as.character)

Converting character (string) to timestamp data format (R friendly)

# converting string to timestamp data format (R friendly)
for (i in 1:length(d$metric_datetime[i])) d$metric_datetime <- strptime(d$metric_datetime[i], "%Y-%m-%d %H.%M.%OS")

Packages

Package Comment
twitter/AnomalyDetection AnomalyDetection R package to detect anomalies which is robust, from a statistical standpoint, in the presence of seasonality and an underlying trend
hadley/devtools devtools makes package development easier by providing R functions that simplify common tasks

devtools and proxy settings

require(httr)
set_config(
  use_proxy(url="18.91.12.23", port=8080, username="user",password="password")
)

One can also add this to .Rprofile for consistency

Examples

# converting string to timestamp data format (R friendly)
for (i in 1:length(d$metric_datetime[i])) d$metric_datetime <- strptime(d$metric_datetime[i], "%Y-%m-%d %H.%M.%OS")

# reordering columns in a data frame
d <- d[c("metric_datetime", "metric_value")]

Rtools

https://cran.r-project.org/bin/windows/Rtools/