-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
115 lines (115 loc) · 2.49 KB
/
.Rhistory
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
setwd("~/Projects/DataScienceCoursera")
#Setting up
x <- c("a", "b", "c", "c", "d", "a")
#Subset indices start at 1
x[1]
x[2]
x[1:4] #Multiple
#logical subset
x[x>"a"]
u<-x>"a" #Save logical subset
u
x[u]
#Matrix
m<-matrix(1:6,2,3)
m
m[1,2]
m[2,1]
m[1,] #get 1st row
m[,2] #get 2nd column
#by default subsetting matrices return a vector, to turn off behavior see below
m[1,2, drop= F]
#by default subsetting matrices return a vector, to turn off behavior see below
m[1,2, drop= F]
m[1, drop= F]
m[1, drop= F]
m[1,2, drop= F]
m[1, ,drop= F]
l <- list (rank = 1:4, bar =0.6, baz="hello")
x[1]# Get foo
l[1]# Get foo
l[[1]]# Get just the sequence
l$bar#get element associated with 'bar'
l[["bar"]]#equivelant to top
l["bar"]
x[c(1,3)] # subset the 1st & 3rd element
l[c(1,3)] # subset the 1st & 3rd element
l[[c(1,3)]] # two [[]] means get one element so this is getting the 3rd element within the 1st element
x[[1]][[3]] # Same as above
l[[1]][[3]] # Same as above
name <-"rank"
x[[name]] ### LOOK cool feature if you compute index use this!
l[[name]] ### LOOK cool feature if you compute index use this!
name <-"rank"
l[[name]] ### LOOK cool feature if you compute index use this!
l$name
x$rank
l$rank
l$r ### Same as above; $ tries to find a match and 'rank' is the closest
nums <- c(1,2,NA, 4, NA, 5)
bad<-is.na(nums) # Creates locical list
x[!bad] # Gets rid of the bad data
nums[!bad] # Gets rid of the bad data
bad<-is.na(nums) # Creates locical list
nums[!bad] # Gets rid of the bad data
## More complex... Subset all missing element
t <- c(1,2,NA, 4, NA, 5)
i <- c("a", "b",NA, "d", "e", "f")
good<- complete.cases(x,y)
good<- complete.cases(t,i)
good
x[good]
t[good]
i[good]
concat<-c(T,T,0,1,6,7)
concat
## Can force obj type
as.logical(concat)
## DF with column names
race<-data.frame(Rank= 1:4, Passed= c(T,T,F,F))
race
## List with column names
raceNum<-1:4
raceNum
names(raceNum)<-c("Bob","Rob","Joe", "Jon")
raceNum
## Matrix with Row & column names
m<-matrix(1:4, nrow=2, ncol=2)
m
dimnames(m)<-list(c("a","b"), c("c","d"))
m
attributes(m)
## Change matrix size
dim(m)<-c(1,4)
m
m
attributes(m)
dimnames(m)<-list(c("a","b"), c("c","d"))
m
## Matrix with Row & column names
m<-matrix(1:4, nrow=2, ncol=2)
m
dimnames(m)<-list(c("a","b"), c("c","d"))
m
attributes(m)
dim(m)<-c(1,4)
m
attributes(m)
## Diff in c & r bind
a<-1:3
b<-10:12
cbind(a,b)
rbind(a,b)
## NA & NaN are different!
l<-c(NA,NaN)
l
is.na(l)
is.nan(l)
###########################
myfunct<-function(){
y<-rnorm(100)
mean(y)
}
sec<-function(x){
x + rnorm(length(x))
}