-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathR code 1a.R
195 lines (141 loc) · 4.22 KB
/
R code 1a.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
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
2 + 2 # control + enter or control + R
# works as calculator
1:50 # print numbers 1 to 50 to the console
50:1 # print numbers 50 to 1 in reverse order to console
print ("Hello World!")
# ctrl + L is going to clear the console
x <- 1:5 # assigning numbers 1 to 5 to the object x
x
y <- c(3, 5, 8, 1, 2) # alt + '-' is the shortcut for assignment operator
y
a <- x + y
a
z <- 4:9
z
x + z
p <- 4:8
p
x + p
x * 2
ls() # list objects
#packages are a collection of R functions
install.packages("XML")
install.packages("XML", repos = "http://www.omegahat.net/R")
library(XML)
version
mba <- read.csv("C:\\Users\\welcome\\Documents\\ExcelR\\Day 05 Basic Stat _ R\\Data Sets\\mba.csv") # load csv file into R
?read.csv
View(mba) # show the dataset uploaded to R
getwd() # shows the current working directory
setwd("C:\\Users\\welcome\\Documents\\ExcelR\\Day 05 Basic Stat _ R\\Data Sets\\") # set a working directory of your choice
str(mba) # quick overview of the variables & dataset
# update.packages()
install.packages("RCurl")
library(RCurl)
data2 <- getURL("https://www.excelr.com")
#data2
browseURL("http://ftp.iitm.ac.in/cran/")
library()
install.packages("xlsx")
# Loading the library of xlsx
library(xlsx)
?require
require(xlsx)
library(help = "xlsx") # it'll open a tab with the help documentation
#Long form of your documentation for the respective pacakages
vignette(package="xlsx")
# Loading the long form of documentation through browser.
browseVignettes(package="xlsx")
vignette()
browseVignettes()
rm(list=ls())
#======================================================================
# vector
temp <- c(38, 32, 34, 38, 40)
#(38+32+34+38+40)/5
#Overall average for the defined dataset provided in the function
mean(1,2,3,4,5)
# middle value of the dataset
median(temp)
x <- c(1, 2, 3, 4, 5)
x
mean(x)
a <- c(1,2,5.3,6,-2,4) # numeric vector
a
b <- c("YES","OK","FINE") # character vector
b
c <- c(TRUE,TRUE,TRUE,FALSE,TRUE,FALSE) #logical vector
c
typeof(x)
#======================================================================
# List An ordered collection of objects
#Loading the value in local variable using
rain <- list('Y', 'N', 'N', 'Y', 'Y')
typeof(rain)
class(rain)
temp <- list(38, 32, 34, 38, 40)
rain
temp
#======================================================================
#Array
array(1:3, c(2,4,2))
b <- array(1:30, c(5, 3, 4))
a
b
#1:4
# another example
cells <- c(1,26,24,68)
rnames <- c("R1", "R2")
cnames <- c("C1", "C2")
mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=TRUE,
dimnames=list(rnames, cnames))
mymatrix
#======================================================================
# matrix
temp <- c(38, 32, 34, 38, 40)
str(temp)
#Priority goes to char then number
percp <- c(110, 102, 103, 117, 90,'Y')
str(percp)
percp <- c(110, 102, 103, 117, 90)
mat = matrix(c(temp, percp), nrow=2, byrow=T)
mat[,4] # 4th column of matrix
#======================================================================
#DataFrame contains different columns can have different modes (numeric, character)
temperptrain <- data.frame(temp = c(38, 32, 34, 38, 40),
percp=c(110, 102, 103, 117, 90),
rain=c('Y', 'N', 'N', 'Y', 'Y'))
typeof(temperptrain)
class(temperptrain)
temperptrain
View(temperptrain)
#======================================================================
#An ordered collection of objects
w <- list(name="ExcelR", mynumbers=temp, mymatrix=rain, age=5.3)
w$age
w$mymatrix
# example of a list containing two lists
v <- c(list1,list2)
raintemp <- list(rain, temp)
raintemp
# for viewing the dataset along with it's type
str(raintemp)
#======================================================================
#user defined function
cube <- function(x){x*x*x}
a <- cube(2)
#Print cube root form the value
a
cube(1:4)
#in-built functions
a <- seq(1, 0, -0.1)
a
seq(1, 0, -0.1)
seq(10, 0, -1)
str(seq)
#======================================================================
#to View built in DAtabases in R
data()
data("EuStockMarkets")
View(EuStockMarkets)
?EuStockMarkets