-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch3_lists.R
85 lines (64 loc) · 1.74 KB
/
ch3_lists.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# ch3_lists
# heterogenous objects
# named / unnamed list
# create a simple, unnamed list
capitals=list('new delhi','tokyo','beijing','london')
capitals
# total elements
length(capitals)
# access the list
capitals[1]
capitals[1:3]
# named list
clr = list('primary' = c('red','green','blue'))
print(clr)
# access the named list - using the $ sign
clr$primary[1]
clr$primary[3]
# convert a list to vector
unlist(capitals)
# adding elements to a list
# by default, values are always added at the end
capitals = append(capitals,c('abc','xyz'))
capitals
# add a new entry after 'new delhi'
# first find the index pos of 'new delhi'
pos = which(capitals == "new delhi")
# append the new value
capitals=append(capitals,"colombo",after=pos)
# change 'abc' into 'jakarta'
capitals[which(capitals=="abc")] = "jakarta"
capitals
# similarly, change "xyz" to "washington dc"
capitals[which(capitals=="xyz")]='washington dc'
capitals
# delete values from list
capitals[7] = NULL
capitals
# create a named list to store my information
# educ type
# insti name
# gpa / Score
# grad_year
myrec=list(
"eductype" = c('school','highschool','grad'),
"institute"=c('lawrence','hindu','aegis'),
"score" = c(88.1,78.9,84.2),
"year" = c(2001,2003,2007))
myrec
# get all the high school details
print(paste("Educ Type=",myrec$eductype[2],"School=", myrec$institute[2],"Score=",myrec$score[2], "Grad Year=",myrec$year[2]))
# merge lists
# lists can contain different data types
list1 = list(c(1,2,3,4,5))
list2 = list(c('a','b','c'))
list3 = c(list1, list2)
list3
# convert list->vector
unlist(list3)
# pre-defined vectors
letters
as.list(letters)
LETTERS
month.name
month.abb