List, Factor
by mervyn
source “K-MOOC 오세종 교수님의 [R 데이터 분석 입문] 강좌의 3-5. list, factor 중 (http://www.kmooc.kr/courses/course-v1:DKUK+DKUK0003+2020_T3)”
List
Various data type can be mixed and stored, which is the difference with vector. You can store vector, data frame as elements
member<- list(name='kim', address='pusan' # name, address, tel, age, married: naming and store the data
tel='010-1234-5678', age=20, married=FALSE) # character, number, matrix can be stored
member
Extract value of element
member[[1]] # type
member$name
member[1] # list
Factor
character type variable similar to vector Data type only takes specific values
blood.type<- factor(c("A", "A", "AB", "O", "B")) # factor(): factor type
blood.type
is.factor(blood.type)
In Level sequence, factor can be transed to number
blood.type
# Levels: Only A AB B O can come for blood type (factor type)
as.numeric(blood.type) # as.numeric: substitute to number
# sequence: sequence in Levels
read.csv() reads file and store character column in factor
Assignment
-
Create list myiris, first element to be “Species” column of iris dataset, second element to be the rest of 4 columns. Name of each element to be “Group”, “Data”
-
Show myiris 1st and 2nd elements’ content
-
Create factor variable weekdays with (Sun, Mon, …) Show the content of weekdays
-
Show weekdays values in numbers
myiris<- list(Group=iris[, "Species"], Data=iris[,c(1:4)]) # 1
myiris[[1]]
myiris[[2]] # 2
weekdays<- factor(c("Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"))
weekdays # 3
as.numeric(weekdays) # 4
Comments
Post comment