Beruflich Dokumente
Kultur Dokumente
Week 3:
Basic concepts II: data structures 1
1/29
1 Goals
2 Data type and data structure
3 Vectors
4 Matrices and arrays
5 Lists
6 Data frames
7 Factors
2/29
1 Goals
2 Data type and data structure
3 Vectors
4 Matrices and arrays
5 Lists
6 Data frames
7 Factors
3/29
4/29
1 Goals
2 Data type and data structure
3 Vectors
4 Matrices and arrays
5 Lists
6 Data frames
7 Factors
5/29
Type in
numeric 2
complex
logical
logical
character
raw
Display
3.27
3+2i
TRUE or FALSE
NA
"text"
1c
This week, we will discuss how to structure data (of multiple data
points) in .
6/29
1 Goals
2 Data type and data structure
3 Vectors
4 Matrices and arrays
5 Lists
6 Data frames
7 Factors
7/29
Vectors - page 51
Vector represents a sequence of data points of the same type. You
can create a vector in different ways
by using the function c().
by using the function seq()
by using a colon :
Run the following codes and see what you get
>
>
>
>
8/29
c(3,1,7)
seq(from=0,to=1,by=0.1)
seq(from=0,to=20,length=5)
(vec <- 2:33)
Vectors - page 51
Results
> c(3,1,7)
[1] 3 1 7
> seq(from=0,to=0.9,by=0.1)
[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
> seq(from=0,to=20,length=5)
[1] 0 5 10 15 20
> (vec <- 2:15) # [12]=rank of the next element.
[1] 2 3 4 5 6 7 8 9 10 11 12
[12] 13 14 15
9/29
Vectors - page 51
Note:
Despite that you can mix data of different types,
implicitly convert them into a single data type
will
> c(3,TRUE,7)
> vec <- 1:10
> names(vec) <- letters[1:10]
10/29
Vectors - page 51
Results
> c(3,TRUE,7) #
[1] 3 1 7
> vec <- 1:10 #
> names(vec) <a b c d e
1 2 3 4 5
Here, the built-in constants letters[1:10] return the first 10 letters of the alphabet.
11/29
1 Goals
2 Data type and data structure
3 Vectors
4 Matrices and arrays
5 Lists
6 Data frames
7 Factors
12/29
>
>
>
>
13/29
(X <- matrix(1:12,nrow=4,ncol=3,byrow=TRUE))
(X <- matrix(1:12,nrow=4,ncol=3,byrow=FALSE))
class(X)
(X <- array(1:12,dim=c(2,2,3)))
14/29
16/29
1 Goals
2 Data type and data structure
3 Vectors
4 Matrices and arrays
5 Lists
6 Data frames
7 Factors
17/29
Lists - page 53
Lists can group together in one structure data of different types without altering them.
>
+
A <- list(TRUE,-1:3,my.matrix=matrix(1:4,
nrow=2),c(1+2i,3),"A character string")
18/29
Lists - page 53
> A
[[1]]
[1] TRUE
[[2]]
[1] -1 0 1 2 3
$my.matrix
[,1] [,2]
[1,]
1
3
[2,]
2
4
[[4]]
[1] 1+2i 3+0i
[[5]]
[1] "A character string"
19/29
Lists - page 54
Results
There are 5 elements.
The third elements name is my.matrix. The rest have default
names that show their positions in the list.
No - this is one advantage of using the list. In fact, each
element can be a vector, a matrix, an array or even a list.
Note: naming element will make it easier to read the data from a list
- we will discuss this further in later sessions.
20/29
1 Goals
2 Data type and data structure
3 Vectors
4 Matrices and arrays
5 Lists
6 Data frames
7 Factors
21/29
Data frames
A data.frame in
is a table where
22/29
Data frames
> (BMI <- data.frame(
+
Gender=c("M","F","M","F"),
+
Height=c(1.83,1.76,1.82,1.60),
+
Weight=c(67,58,66,48),
+
row.names=c("Jack","Julia","Henry"
+
,"Emma")))
Gender Height Weight
Jack
M
1.83
67
Julia
F
1.76
58
Henry
M
1.82
66
Emma
F
1.60
48
23/29
Data frames
24/29
1 Goals
2 Data type and data structure
3 Vectors
4 Matrices and arrays
5 Lists
6 Data frames
7 Factors
25/29
Factors
A factor can be used to store character strings
each element is treated as a factor (even if the input is a real
number)
some functions require data structured as a factor
Try the following example
26/29
Factors
Other structures
You can use the as.Date function for dates
> dates<-c("92/27/02","92/02/27")
> as.Date(dates,"%y/%m/%d")
[1] NA
"1992-02-27"
You can also create a time series structure
> ts(1:10,frequency=4,start=c(1959,2))
Qtr1 Qtr2 Qtr3 Qtr4
1959
1
2
3
1960
4
5
6
7
1961
8
9
10
28/29
Instruction
in
vector
matrix
c()
matrix()
array
list
data frame
factor
dates
time series
29/29
Description