The R Notes - Part 2

Sat February 8, 2020
r

Factors

Factors are used to store categorical data, ordered or unordered. Can be viewed as an integer vector where each entry has a label, better that categorizing with integers.

x<- factor(c("low", "high", "high", "high", "low", "low", "high", "high"))
print(x)

will store the values in x. It will also tell you the labels in the list:

[1] low  high high high low  low  high high
Levels: high low

Calling the table function will tell you the frequency of the tables

print(table(x))
high  low 
   5    3 

unclass method will trip the labels from the factor:

print(unclass(x))
[1] 2 1 1 1 2 2 1 1
attr(,"levels")
[1] "high" "low"

notice that it is an integer vector order of labels can be set using the levels argument

x<- factor(c("low", "high", "high", "high", "low", "low", "high", "high"))
print(x)

y<- factor(c("low", "high", "high", "high", "low", "low", "high", "high"),levels = c("low", "high"))
print(y)
[1] low  high high high low  low  high high
Levels: high low
[1] low  high high high low  low  high high
Levels: low high

NaN and NA

NaN: undefined mathematical operations NA: The rest

to detect use .isnan() or .isna()

Names

Objects can have names to describe them. Consider

x<-1:3
print(x)

print(names(x))
[1] 1 2 3
NULL

Assigning names

names(x)<- c("stephen", "colin", "mark")
print(x)
stephen   colin    mark 
      1       2       3 

Manipulating elements

print(x["stephen"]+7)

print(x[1]+7)
stephen 
      8 

stephen 
      8 

Naming elements in Lists

y<-list("stephen" = 12, "colin" = TRUE, "mark" = "hello")

print(y)
$stephen
[1] 12

$colin
[1] TRUE

$mark
[1] "hello"

Elements in Matrices can be named using the dimnames() function

dimnames(m)<- list(c("Joe", "Peter"), c("Maths", "English", "Physics"))
print(m)

print(m["Peter", "English"])
      Maths English Physics
Joe       1       3       5
Peter     2       4       6

[1] 4



The R Notes - Part 3

February 8, 2020
r

The R Notes - Part 1

February 8, 2020
r
comments powered by Disqus


machine-learning 27 python 21 fuzzy 14 azure-ml 11 hugo_cms 11 linear-regression 10 gradient-descent 9 type2-fuzzy 8 type2-fuzzy-library 8 type1-fuzzy 5 cnc 4 dataset 4 datastore 4 it2fs 4 excel 3 paper-workout 3 r 3 c 2 c-sharp 2 experiment 2 hyperparameter-tuning 2 iot 2 model-optimization 2 programming 2 robotics 2 weiszfeld_algorithm 2 arduino 1 automl 1 classifier 1 computation 1 cost-functions 1 development 1 embedded 1 fuzzy-logic 1 game 1 javascript 1 learning 1 mathjax 1 maths 1 mxchip 1 pandas 1 pipeline 1 random_walk 1 roc 1 tools 1 vscode 1 wsl 1