Thursday, 13 April 2017

Introduction to R


NOTE: WORK IN PROGRESS - THIS IS AN UNFINISHED ARTICLE

Variables


To assign a value to some variable we have to use assignment operator <-:
> s <- "This is some string" > s
[1] "This is some string"

If variable table contains rows and columns from some table (matrix) and columns have names like column1, column2 etc...we can access columns as variables if we use dollar sign notation:

table$column1

We can also use dollar sign notation to add a new column to the table:

table$column1_log <- apply(table[, "column1"], 1, log)



> typeof(credit_samples[1])
[1] "list"
> typeof(credit_samples)
[1] "list"
> typeof(credit_samples[[1]])
[1] "environment"


File System


~/ is Home (User) Directory in Linux and expands to /home/username. This shortcut is very convenient as it hides absolute path (and user name).

normalizePath()

String operations


paste0

cat


Data Exploration


print

colnames

summary(data)

Output:
Column_name
Min. :1.000
1st Qu.:1.000
Median :2.000
Mean :2.577
3rd Qu.:4.000
Max. :4.000
NA's :4


head

nrow

To find out elements which belong to one but not to another set we can use setdiff:
> a <- 1:5 > a
[1] 1 2 3 4 5
> b <- 3:8 > b
[1] 3 4 5 6 7 8
> setdiff(a, b)
[1] 1 2
> setdiff(b, a)
[1] 6 7 8

Data Manipulation


c - combines its arguments to form a vector

> v <- c(1, 2, 3) > print(v)
[1] 1 2 3
> for(i in v) print(i)
[1] 1
[1] 2
[1] 3

To transform specific elements from data frame or elements in bulk (entire row or column) use apply(X, MARGIN, FUN, ...). X is vector or matrix, MARGIN is a vector with indices determining on which rows, columns or elements function FUN shall be applied. Set MARGIN to 1 to denote rows, 2 to denote columns, c(1, 2) to denote specific element in 1st row and 2nd column.

apply(data[, "Credit Amount"], 1, log)
C1
1 6.955593
2 7.937017
3 6.734592
4 7.660114
5 7.682943
6 7.714677

[1000 rows x 1 column]

setdiff

https://stat.ethz.ch/R-manual/R-devel/library/base/html/normalizePath.html
https://wiki.mobilizingcs.org/rstudio/examining_data
https://www.stat.berkeley.edu/~spector/R.pdf
https://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html

Post a Comment