-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR1.R
More file actions
225 lines (154 loc) · 3.06 KB
/
Copy pathR1.R
File metadata and controls
225 lines (154 loc) · 3.06 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Basics of R
a <- 5 # crtl + enter
a
a = 6
a
b <- "this is string"
b
class(a)
class(b)
#simple calculator
5*3
4+3
# '<-' and '=' both operators perform same
x <- 3
y = 4
x+y
#.....combination values
a=c(2,3,4)
mean(a)
A <- c("1","2","3","4","5")
A
class(A)
d <- c(1,2,"g")
class(d)
# character > numeric > logical T=1 and F=0
h <- c( T, FALSE, 7,8, "Shweta")
class(h)
#.....Sequence of series
##generates the integers from 1 to 10
x1 <- 1:10
x1
#sequence ...
x2 <- seq(from =1, to = 20, by = 2)
x2
seq(1,12,1)
x3 <- seq(from =12, to = 20, length = 3)
x3
# repeat the number 3, 5 times
rep(3,5)
#length of vector
length(A)
# R uses NA to represent missing value
x=c(2,1,NA,3)
sum(x)
#calculate exluding NA
sum(x, na.rm=T)
# to get help of sum function
help(sum)
?sum
#lists of objects available in current workspace
ls()
#remove object
rm(a)
#install package
install.packages("caret")
#remove package
remove.packages("caret")
#use installed package (load package)
library(caret)
#unload library
detach(caret)
#Import CSV File to R
oj <- read.csv("C:/Users/shwetag/Downloads/oj.csv",header=TRUE)
oj
#save data in csv formate (export csv file)
write.csv(oj, "file1.csv")
#to check working directory (where this data is save)
getwd()
#mathematical functions
x=c(3,4,7,9,2,5,8)
log(x)
exp(x)
sqrt(x)
median(x)
mean(x)
plot(log(x))
plot(log(x), type = "l")
plot(log(x), type = "p")
plot(sqrt(x), type = "l")
plot(exp(x), type = "l")
plot(exp(w), type = "l", xlim = c(0,15), ylim = c(10,1000))
#....Data types in R
# vector-------------
#integer
x <- 1:10
x
class(x)
#character
a <- c("apple","apple" ,"mango", "orange")
a[4]
class(a)
mean(x) #calculate mean
table(a) #calculate frequency
sum(x[c(3,5)]) # sum of 3rd and 5th number
#Convert a column "x" to numeric
x = as.numeric(x)
class(x)
# factor --------------------------
f <- rep(c(1,2,3),4)
f
class(f)
f <- as.factor(f)
class(f)
levels(f)
table(f)
#matrices--------------
c <- matrix(1:20, nrow = 4, ncol = 5,byrow = FALSE)
c
a=matrix(c(2,3,6,4),2,2)
a
class(a)
x1=c(1,2,3)
x2=c(4,5,6)
x3=c(7,8,9)
M=cbind(x1,x2,x3)
M
#operations on matrix
t(a) #t(M) means tranpose
mult = a %*% t(a) #multiplication
mult
add=M + M #addition
add
dim(M) #dimensions of matrix
# Arrays -----------------
3*3*2
e <- array(1:10, dim = c(3,3,2)) #[row, column, mutiple]
e
# list ----------------------
b <- list(id=c(1,2,3), name =c("a","b", "c", "d") , level = c(T,F,T,T,T))
b[[1]]
b[[2]]
b[1]
b
b$id
b$name
b["id"]
# data frame -------------------------
var1 = c("a","b","c")
var2 = c(1:3)
var3 = c(1999:2001)
var4 = c(T,T,F)
mydata <- data.frame(var1,var2,var3,var4)
mydata
#Operations on data
View(mydata)
mean(mydata$var2)
min(mydata$var3)
sqrt(mydata$var3)
hist(mydata$var2)
#..data slicing
#mydata[row,column]
mydata[2,3]
mydata[,c(2,3)]
mydata[1:2,]