Repository navigation
Expand file tree
/
Copy pathR Programming Practice codes
More file actions
319 lines (239 loc) · 6.79 KB
/
Copy pathR Programming Practice codes
File metadata and controls
319 lines (239 loc) · 6.79 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# For loop in R
directory <- "specdata"
file_list <- list.files(directory)
file_list
da = read.csv(file_list[1])
#comp_file <- data.frame()
comp_file <- rbind(comp_file, read.csv(file_list[1]))
id = 1:332
for (i in 1:332) {
comp_file <- rbind(comp_file, read.csv(file_list[i]))
comp_file
}
# Statistical Inference module - 1 using Swirl R package
install.packages("swirl")
packageVersion("swirl")
library(swirl)
install_from_swirl("Statistical Inference")
swirl()
install.packages(file.choose(), repos = NULL)
?qunif()
qunif(0.75, min = 0, max = 3, lower.tail = TRUE, log.p = FALSE)
x <- 1:4; x
p <- x/sum(x); p
temp <- rbind(x, p); temp
rownames(temp) <- c("X", "Prob")
temp; temp
mean = sum(x*p); mean
(0.3*0.75)/( (0.3*0.75) + (0.7*0.25))
##########################################################################################################################
# Just for practice code
x <- 4
class(x)
x <- c(4, "a", TRUE)
class(x)
x <- 1:4
y <- 2:3
x+y
class(x + y)
x <- c(1,3, 5)
y <- c(3, 2, 10)
rbind(x,y)
x <- list(2, "a", "b", TRUE)
class(x[[1]])
x = 1:4
y = 2:3
class(x+y)
x <- c(3, 5, 1, 10, 12, 6)
x[x < 6] <- 0
x
x <- c(3, 5, 1, 10, 12, 6)
x[x < 6] == 0
x
# Suppose I have a vector x <- c(17, 14, 4, 5, 13, 12, 10) and I want to set all elements of this vector that are greater than 10 to be equal to 4. What R code achieves this?
# x[x < 10] <- 4
# x[x >= 11] <- 4
# x[x > 4] <- 10
# x[x == 10] <- 4
x <- c(17, 14, 4, 5, 13, 12, 10)
x[x >= 11] <- 4
x
# To create a varible named "mydata", which holds excel data given in "hw1_data R programming" excel file
mydata <- read.xls("hw1_data R programming.xlsx")
# this data is loaded in enviornment.
# To print mydata.
mydata
# ALternate way of printing mydata is print (mydata)
print (mydata)
#read.csv("hw1_data R programming.csv")
# read.csv ("" ) is alternate way for reading excel files which are saved in csv format
# read.table ("") is alternate way of for reading excel files ...
# To view structure of the data frame named "mydata"
str(mydata)
mydata[152, ]
mydata[153, ]
nrows(mydata)
# To print only the column named Ozone
mydata$Ozone
#To check which values of this column are NA values and print them
NA_Ozone = is.na(mydata$Ozone)
NA_Ozone
To subset dataframe named "mydata" for multiple conditions on different columns
subset1 <- subset(mydata, Ozone > 31 & Temp > 90)
class(subset1)
mean(subset1$Solar.R)
!is.na(subset1)
subset2 = subset(mydata, Month = 6)
mean(subset2$Temp)
subset3 = subset(mydata, Month = 5)
na.omit(subset3$Ozone)
max(na.omit(subset3$Ozone))
max(!is.na(subset3$Ozone))
mean(!is.na(subset(mydata, Month = 6)))
mean(as.numeric(!is.na(subset1)))
numric_subset1 = as.numeric(subset1)
mean(numric_subset1)
#Suppose I define the following function in R
cube <- function(x, n) {
x^3
}
#What is the result of running
cube(3)
# The following code will produce a warning in R.Why?
x <- 1:10
if(x > 5) {
x <- 0
}
x
# The options are: There are no elements in 'x' that are greater than 5
#The expression uses curly braces.
#You cannot set 'x' to be 0 because 'x' is a vector and 0 is a scalar.
#The syntax of this R expression is incorrect.
#'x' is a vector of length 10 and 'if' can only test a single logical statement.
# For the function
f <- function(x) {
g <- function(y) {
y + z
}
z <- 4
x + g(x)
}
#If I then run in R
z <- 10
f(3) # What value is returned?
x <- 5
y <- if(x < 3) {
NA
} else {
10
}
y
#Which symbol in the following function is a free variable? Answer is 'f'
h <- function(x, y = NULL, d = 3L) {
z <- cbind(x, d)
if(!is.null(y))
z <- z + y
else
z <- z + f
g <- x + y / z
if(d == 3L)
return(g)
g <- g + 10
g
}
#lexical scoping
y <- 10
f = function(x){
y <- 2
y^2 + g(x)
}
g = function(x){
x*y
}
f(3)
library(datasets)
data(iris)
?iris
str(iris)
sepal_length = iris$Sepal.Length
mean(sepal_length)
subset2 = subset(iris, Species == virginica, select = Sepal.Length )
# subset3 = iris[Species == virginica, ]
subset2
sepal_length_virginica
ls
debug(ls)
ls
apply(iris[, 1:4], 2, mean)
apply(iris, 2, mean)
apply(iris[, 1:4], 1, mean)
library(datasets)
data(mtcars)
?mtcars
lapply(mtcars, mean)
with(mtcars, tapply(mpg, cyl, mean))
sapply(mtcars, cyl, mean)
tapply(mtcars$cyl, mtcars$mpg, mean)
## Practice codes for matrices, data frame, subsetting loops, functions etc.
# https://github.com/rdpeng/practice_assignment/blob/master/practice_assignment.rmd
dataset_url <- "http://s3.amazonaws.com/practice_assignment/diet_data.zip"
download.file(dataset_url, "diet_data.zip")
unzip("diet_data.zip", exdir = "diet_data")
andy <- read.csv("diet_data/Andy.csv")
head(andy) #will give you the 6 first rows
tail(andy) #will give you the 6 last rows
str(andy) # will give you structure of the df andy
dim(andy) # will give you dimension of the df andy
summary(andy) # will give you summary of the df andy
names(andy) # will give you the names in the data df named andy
length(andy$Day)
length(andy$Age)
nrow(andy) #will give you the numbers of rows in the df named andy
n <- nrow(andy)
andy[(n - 3 + 1):n, ] # will give you the last 3 rows
# Specifically, the first row of the 'Weight' column:
andy[1, "Weight"]
#We can do the same thing to find his final weight on Day 30:
andy[30, "Weight"]
# Alternatively, you could create a subset of the 'Weight' column where the data where 'Day' is equal to 30.
andy[which(andy$Day == 30), "Weight"]
andy[which(andy[,"Day"] == 30), "Weight"]
#Or, we could use the subset() function to do the same thing:
subset(andy$Weight, andy$Day==30)
# Let's assign Andy's starting and ending weight to vectors:
andy_start <- andy[1, "Weight"]
andy_end <- andy[30, "Weight"]
#We can find out how much weight he lost by subtracting the vectors:
andy_loss <- andy_start - andy_end
andy_loss
?list.files
list.files()
files = list.files("diet_data") # file is now a list of contents of diet_data in alphabetical order
files # No matter in what order files are saved in your diet_data folder in current working directory.
class(files)
files[1]
files[2]
files[3:5]
head(read.csv(files[3]))
# To append the path of the file name
files_full <- list.files("diet_data", full.names=TRUE)
files_full
head(read.csv(files_full[3]))
# To create a dataframe
andy_david <- cbind(andy, read.csv(files_full[2]))
#This line of code took our existing data frame, Andy, and added the rows from David.csv to the end of it. We can check this with:
head(andy_david)
tail(andy_david)
for (i in 1:5) {
dat <- rbind(dat, read.csv(files_full[i]))
}
#Whoops. Object 'dat' not found. This is because you can't rbind something into a file that doesn't exist yet. So let's create an empty data frame called 'dat' before running the loop.
dat <- data.frame()
for (i in 1:5) {
dat <- rbind(dat, read.csv(files_full[i]))
}
str(dat)
dat_30 <- dat[which(dat[, "Day"] == 30),]
dat_30
median(dat_30$Weight)
tail(andy_david)