0% found this document useful (0 votes)
2 views

CIND123 Module 2

The document provides an overview of data manipulation and types in R, including vector indexing, data types such as numeric, character, date, and logical, as well as matrix creation and manipulation. It explains how to create and modify vectors and matrices, and how to utilize functions like 'factor' for categorical data. Additionally, it covers basic operations and logical comparisons in R.
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CIND123 Module 2

The document provides an overview of data manipulation and types in R, including vector indexing, data types such as numeric, character, date, and logical, as well as matrix creation and manipulation. It explains how to create and modify vectors and matrices, and how to utilize functions like 'factor' for categorical data. Additionally, it covers basic operations and logical comparisons in R.
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

 Module 2

 Length(x)
 x[3] gets the third element of x
 y <- x[-2]
 Skips the second element of a vector
 y <- c(x[1:3], 300, x[4])
 x{1:3] is the first few elements of a vector
 z <- x[-c(1:3)]
 Excludes elements 1:3
 Data types in R
 Numeric
 Character
 Date
 Logical
 Numeric
 x = 1.5
 class(x)
 Or is.numeric(x)
 Is.integer
 R will store y=10 as an integer
 use z = as.integer(15)
 Character data
 is.character(x)
 Factor command
 y <- factor(“data”)
 Date
 Date1 <- as.Date(“2012-06-28”)
 class(date1)
 as.Date(05/06/2007”, “%m/%d?%Y”)
 Custom date orders
 True = 1
 True * 5 = 5
 Logical Data
 k <- TRUE
 class(k)
 “logical”
 > 2 == 3 (false)
 >2 != 3 (true)
 Matrices
 m (rows) by n (column)

> x <- 1:12


> dim(x)<- c(3,4)
>x
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> x[,1]
[1] 1 2 3
> matrix(1:12), nrow = 3, byrow = T
Error: unexpected ',' in "matrix(1:12),"
> matrix(1:12, nrow = 3, byrow = T)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
> matrix(1:12, nrow = 3, ncol=3 )
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> cbind(A=1:4, B=5:8, C=9:12)
AB C
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
> rbind(A=1:4, B=5:8, C=9:12)
[,1] [,2] [,3] [,4]
A 1 2 3 4
B 5 6 7 8
m[-2,]
(eliminates second row)

 mean(asdasd, na.rm=T)
 Use factor for ordinal data

You might also like