The R Notes - Part 2

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
hugo_cms 11 fuzzy 9 python 8 machine_learning 5 cnc 4 type1_fuzzy 4 type2_fuzzy_library 4 r 3 type2_fuzzy 3 excel 2 iot 2 it2fs 2 weiszfeld_algorithm 2 arduino 1 classifier 1 development 1 game 1 javascript 1 learning 1 mathjax 1 maths 1 mxchip 1 pandas 1 random_walk 1 robot 1 roc 1 set 1 tools 1 type_2_fuzzy 1 vscode 1 wsl 1