Data Journey

International Studies Grad. racing for AI Engineer

Download as .zip Download as .tar.gz View on GitHub
31 January 2021

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

  1. 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”

  2. Show myiris 1st and 2nd elements’ content

  3. Create factor variable weekdays with (Sun, Mon, …) Show the content of weekdays

  4. 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

back next Index

tags:

Comments

Post comment
Loading...

Share this: