SESSION TWO
Table of Contents
2 SESSION TWO.
2.1 Working with FACTORS in R
2.1.1 How to create FACTORS in R
2.1.2 How to convert FACTOR to numeric variable.
2.2 Subscripting in R
2.1.1 Logical Subscripts
2.1.2 Subscripting Matrices
2.1.3 Create list in R
2.3 Character Manipulation
2.3.1 How to use function paste in R
2.3.2 Breaking apart character Values
2.3.3 Substitutions in R
2 SESSION TWO.
2.1 Working with FACTORS in R.
Factor function is used to create factor. It can be numeric or character variables. Only required argument for factor is a vector of values which will be returned as a vector of factor values.
Level function shows possible levels for a factor.
2.1.1 How to create FACTORS in R.
Use this table as guide.
Factors |
HCl |
NaCl |
KCl |
NaOH |
Numbers of bottles (NB) |
5 |
2 |
3 |
1 |
BioSafetyLvl (BL) |
2 |
1 |
1 |
2 |
Quantity (Q) |
100ml |
20ml |
250ml |
800ml |
Cost (C) |
$10,000 |
$200.46 |
$5,000.12 |
$1,000.15 |
Give values for first part of table (numbers of buttles)
NB=c(5,2,3,1)
fNB=factor(NB)### Convert to factor by using function factor
fNB ### Check factor and levels.
It shows different level of numbers or characters in data. Levels start from minimum to maximum if numbers used.
BL<- c(2,1,1,2) ### The same with Bio Safety level
fBL=factor(BL)
fBL
Levels can be shown separately by using levels function
levels(BL) ### will not give result, because different mode is used (numeric)
levels(fBL) ###levels function is used with factors only, but can be converted to numeric.
2.1.2 How to convert factor to numeric variable.
Create factor
Q=c(100, 20, 250, 800)
Use factor with order function to see difference between levels
Q=factor(cp,levels=c(20,100,250,800),ordered=T)
Q ### See obtained results
mean(Q) ### Calculate mean of Q (It doesn’t work because factor used)
To calculate mean factor should be converted to numeric by using function as.numeric
mean(as.numeric(levels(Q)[Q]))
Or also can be used as:
mean(as.numeric(as.character(Q)))
2.2 Subscripting in R
For objects that contain more than one element (vectors, matrices, arrays, data frames, and lists)
2.2.1 Logical Subscripts
Q ### Let’s use Q
Use function ”>” or “<” To see all data which is bigger or smaller than mean values (or another value)
Q>292
Q1=Q[Q>292] ### Use this function to see or/and save just biggest value
Display obtained value
Q1
Using which function we can determine position of value of interest
which(Q>292)
OR
which(Q<292)
To replace values under 292 to 0 use next function:
Q[Q<292]=0
Q
Use function below to restore values of Q
Q=c(100, 20, 250, 800)
Q
2.2.2 Subscripting Matrices
Create matrices using values saved in Q and BL
x=matrix(Q,BL)
x
x[,2]<292 ### We can manipulate with obtained matrices to see different aspects of values
Extract of all of the off-diagonal elements, by using row number and column number to see if it different
offd=row(x) !=col(x)
offd
2.2.3 Create list in R
To create list in R use list function:
P=list(a=c('HCl','NaCl','KCl','NaOH'), b=c(100,20,250,800))
P ### Use to see obtained data
P[2] ### Used to part of list separately
Use function mode to determine mode used
mode(P[2])
Calculate mean values:
mean(P[2]) ### (will not work because list contain numeric and characters)
mean(P$b) ### $ can be used to show to program than numeric (can be characters) is interest
### Or double [] can be used (the same meaning)
mean(P[[2]])
2.3 Character Manipulation
2.3.1 How to use function paste in R
To paste same characters use paste function:
paste(Q,'ml',sep='')
Q
Q1=paste(Q,'ml',sep='') ### To save obtained result
Q1
paste('ml',Q,sep='') ### Position of character in the in parentheses will dictate position in the results
2.3.2 Breaking apart character Values
How break one sentence for different parts?
For example we have sentence showing below:
F='Factors HCl CuCl KCl NaOH'
To separate this sentence use substring function, where first “c” shows start of word and second end of word (characters)
F1=substring(F,c(1, 9, 13, 18, 22),c(7, 11, 16, 20, 25))
F1 ### Display obtained result
To replace “CuCl” for the “NaCl” we can use next function by showing position of start and end of characters, which is require to be replace
substring(F,13,16)='NaCl'
F ### Use to see obtained result
Repeat following steps:
F1=substring(F,c(1, 9, 13, 18, 22),c(7, 11, 16, 20, 25))
F1
Also easy way can be used by function strsplit which makes break between characters which placed into that characters “ ”
F2=strsplit(F," ")
F2 ### Display obtained result
Use function as.character to convert to character
F3=as.character(F2[[1]])
F3 ### Display obtained result
2.3.3 Substitutions in R
### How to convert data below to numeric and calculate sum?
C=c('$10,000', '$200.46', '$5,000.12', '$1,000.15')
C ### Display obtained result
Use function gsub ### to substitute $ and comma and as.numeric to convert to numeric mode
C1=as.numeric(gsub('[$,]','',C))
C1 ### Display obtained result
sum(C1) ### Calculate sum
Author: Ruslan <khamadalrf@titan.sfasu.edu>