These exercises all use the same data frame we used to demo earlier(data/inflammation-01.csv
). First we need to load and save it as a variable dat
dat <- read.csv(file = "data/inflammation-01.csv", header = FALSE)
# 1.
dat[4,2]
## [1] 0
# 2.
dat[7,5]
## [1] 4
# 3.
dat[10,]
## V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20
## 10 0 1 1 2 1 3 5 3 5 8 6 8 12 5 13 6 13 8 16 8
## V21 V22 V23 V24 V25 V26 V27 V28 V29 V30 V31 V32 V33 V34 V35 V36 V37 V38
## 10 18 15 16 14 12 7 3 8 9 11 2 5 4 5 1 4 1 2
## V39 V40
## 10 0 0
#4.
dat[,3]
## [1] 1 2 1 2 1 1 2 1 0 1 0 0 2 0 2 1 0 0 2 2 1 2 0 1 1 2 1 1 2 0 1 2 2 2 1
## [36] 0 2 1 0 1 0 1 1 1 1 2 2 1 1 1 2 2 0 2 2 1 1 1 0 1
# 1.
mean(as.numeric(dat[1,]))
## [1] 5.45
# 2.
median(dat[,5])
## [1] 2
# 3.
max(dat[10,]) - min(dat[10,])
## [1] 18
# 4.
range(dat[10,])
## [1] 0 18
#5.
quantile(as.numeric(dat[6,]),0.75) - quantile(as.numeric(dat[6,]),0.25)
## 75%
## 6
whichPatients <- seq(2,40,2)
whichDays <- c(1:5)
dat2 <- dat
dat2[whichPatients,whichDays] <- dat2[whichPatients,whichDays]/2
# 1.
apply(dat[1:5, ], 1, mean)
## 1 2 3 4 5
## 5.450 5.425 6.100 5.900 5.550
# 2.
apply(dat[, 1:10], 2, mean)
## V1 V2 V3 V4 V5 V6 V7 V8
## 0.000000 0.450000 1.116667 1.750000 2.433333 3.150000 3.800000 3.883333
## V9 V10
## 5.233333 5.516667
# 3.
apply(dat[, seq(1,40, by=2)], 2, mean)
## V1 V3 V5 V7 V9 V11 V13
## 0.000000 1.116667 2.433333 3.800000 5.233333 5.950000 8.350000
## V15 V17 V19 V21 V23 V25 V27
## 8.366667 9.583333 11.566667 13.250000 11.033333 10.000000 9.150000
## V29 V31 V33 V35 V37 V39
## 7.333333 6.066667 5.116667 3.300000 2.483333 1.133333
sd_day_inflammation <- apply(dat,2, sd)
plot(sd_day_inflammation)
plot(sd_day_inflammation, xlab = "Day")
plot(sd_day_inflammation, ylab = "SD")
filenames <- list.files(path = "data", pattern = "inflammation.*csv", full.names = TRUE)
filenames<-filenames[1:5]
for (f in filenames) {
dat <- read.csv(file = f, header = FALSE)
median_patient_inflammation <- apply(dat, 1, median)
print(median_patient_inflammation)
sd_patient_inflammation <- apply(dat, 1, sd)
print(sd_patient_inflammation)
}
## [1] 4.5 4.5 5.0 5.5 5.0 5.0 5.0 5.0 6.0 5.0 5.0 5.0 5.5 5.0 4.0 5.5 5.5
## [18] 4.5 6.0 6.0 4.0 5.5 5.0 5.0 5.0 7.0 4.0 5.0 5.0 5.0 6.0 5.5 6.0 5.5
## [35] 5.0 4.5 5.0 5.0 5.0 4.0 5.0 5.0 5.5 5.5 6.0 5.0 6.5 5.0 6.0 5.0 5.0
## [52] 5.0 6.0 6.0 5.0 5.0 5.0 6.0 6.5 5.0
## [1] 3.948060 4.378780 4.818767 4.325239 4.107217 4.801108 4.257648
## [8] 5.284472 4.672272 5.124038 5.096442 4.374753 4.293630 4.476434
## [15] 4.281669 4.541546 4.935221 4.756642 4.481014 5.158762 4.922411
## [22] 4.050245 4.861610 4.656399 4.804912 4.722491 5.103794 4.752799
## [29] 4.861610 5.123851 4.396313 4.537875 4.373507 4.255841 5.617680
## [36] 4.534695 4.134827 5.051276 5.004549 4.694514 4.720319 4.596473
## [43] 4.552824 4.561826 4.522026 3.918317 5.441943 4.588447 4.205918
## [50] 4.862138 3.864367 5.011781 4.733338 4.141643 5.088889 4.058546
## [57] 4.908130 4.533635 4.771443 4.601003
## [1] 6.0 5.0 5.0 4.5 5.0 5.0 5.5 5.0 4.0 4.0 5.0 6.0 5.0 5.0 5.5 4.5 5.0
## [18] 5.0 5.0 6.0 6.5 4.5 5.0 6.0 6.0 5.5 5.0 4.5 6.0 4.0 5.5 5.0 4.0 5.0
## [35] 5.5 4.0 4.5 5.0 5.0 5.0 5.5 4.0 4.0 6.0 5.5 4.5 4.5 6.5 6.5 3.5 4.0
## [52] 5.0 4.5 4.0 7.0 4.5 5.0 5.0 6.0 6.0
## [1] 4.341511 4.039548 4.539287 4.287056 4.814242 4.381707 4.583974
## [8] 3.882158 4.054437 4.921890 4.236290 4.710531 4.803778 4.643095
## [15] 4.463815 4.408544 4.196992 4.599261 4.830459 4.098076 3.655344
## [22] 4.376437 4.852636 4.607408 4.418783 5.017853 3.323479 3.925917
## [29] 3.869672 5.229110 4.661627 4.724188 4.657431 4.614289 4.714340
## [36] 3.868346 5.009670 3.869340 5.237378 4.868528 4.722491 5.222240
## [43] 4.945796 4.939052 4.843645 4.647580 4.461804 5.153478 4.493585
## [50] 4.577257 4.031208 4.506406 3.447779 4.771644 4.924038 4.703994
## [57] 4.503489 5.203303 5.302152 4.800574
## [1] 6.0 5.0 5.5 4.5 4.0 5.0 4.0 6.0 5.0 5.0 6.0 5.5 5.0 5.5 6.0 3.5 5.5
## [18] 4.0 6.0 5.0 4.0 4.0 4.5 6.0 5.5 6.5 5.0 6.0 7.0 4.0 5.5 4.0 4.0 4.0
## [35] 5.5 5.5 4.0 6.0 5.0 5.0 6.0 5.0 5.5 4.0 6.0 5.0 6.0 6.0 5.0 5.0 5.0
## [52] 5.0 5.5 5.0 5.0 4.0 5.0 5.5 5.5 5.0
## [1] 4.770637 5.794560 4.896624 4.156860 4.517700 4.253505 4.193936
## [8] 4.545779 4.527976 4.247171 4.557327 4.554443 3.972260 4.647580
## [15] 4.054437 5.133100 5.186212 3.990293 4.971650 5.115086 4.864774
## [22] 3.864284 5.413125 4.828070 4.780033 5.339740 5.111011 4.733676
## [29] 4.446549 4.418783 4.768957 3.869672 4.506335 5.002307 5.200530
## [36] 4.968555 4.595148 4.451375 4.623726 4.662384 4.349993 3.862293
## [43] 4.245963 4.020460 4.301163 4.754485 3.920688 4.087504 4.903622
## [50] 5.528249 3.986837 3.500092 5.310307 3.935294 4.887910 5.191154
## [57] 4.851051 3.789239 4.788876 4.395802
## [1] 5.0 5.5 4.0 5.0 5.0 6.0 6.0 6.0 5.5 6.0 5.0 6.0 5.0 5.0 6.5 4.5 5.0
## [18] 5.5 6.0 4.0 5.5 5.0 6.0 5.5 4.5 6.0 5.0 6.0 4.0 5.5 5.5 5.0 5.0 4.5
## [35] 5.0 5.0 5.5 4.5 5.5 4.0 6.0 5.5 5.0 6.0 5.0 3.0 4.5 5.0 4.0 5.0 4.0
## [52] 4.5 5.0 6.0 5.5 5.0 4.5 4.0 4.0 5.0
## [1] 4.175555 4.647511 4.531301 4.217181 5.583630 4.856861 5.436758
## [8] 5.103983 4.689322 4.733338 4.568496 4.824550 4.877407 4.434639
## [15] 4.889549 4.687066 4.732593 4.440778 5.286594 4.442222 4.850588
## [22] 4.620953 5.030611 5.270868 4.704335 4.640333 4.785394 4.233490
## [29] 4.890532 5.078802 4.665682 4.194013 4.237803 4.700723 4.533635
## [36] 5.165902 3.980642 4.187818 4.550571 5.237684 4.254033 5.460288
## [43] 3.638822 4.309724 4.379366 5.622243 4.384632 4.961131 4.870042
## [50] 4.310913 4.609842 5.288534 4.702427 4.643647 5.007686 4.729612
## [57] 4.475288 3.799376 4.739293 4.679196
## [1] 5.5 5.5 4.5 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.0 5.0 4.5 6.0 4.0 5.0 5.5
## [18] 6.0 6.0 7.0 5.0 4.0 5.0 5.0 7.5 4.5 4.5 6.0 4.5 5.0 4.5 6.0 5.0 5.0
## [35] 5.0 5.0 6.0 6.0 7.0 6.0 4.0 5.5 6.0 5.5 5.5 5.5 4.0 4.0 5.5 5.0 6.5
## [52] 6.5 4.5 6.0 6.0 5.0 5.0 5.0 5.5 5.0
## [1] 3.710605 4.812111 5.364652 5.302152 4.603232 4.801108 4.750169
## [8] 4.231672 4.540134 4.859500 4.861149 4.110961 4.060757 4.727443
## [15] 3.489820 4.293033 4.776008 5.006662 4.970683 5.275974 4.043910
## [22] 5.151487 4.242565 4.997371 4.701336 5.374202 4.670626 4.717602
## [29] 3.720697 4.419073 3.896004 4.955184 4.904145 5.025831 3.318943
## [36] 4.550571 4.062335 4.490446 4.861940 5.260155 4.443087 5.581506
## [43] 4.813909 4.368888 3.617621 4.965522 4.749089 5.088889 4.882662
## [50] 5.148064 5.043147 4.471491 5.310790 3.890818 4.539781 3.849409
## [57] 3.654204 4.505766 4.851381 5.071223
for(i in 1:10){
if(i < 5){
print(i*3)
} else {
print(i*-3)
}
}
## [1] 3
## [1] 6
## [1] 9
## [1] 12
## [1] -15
## [1] -18
## [1] -21
## [1] -24
## [1] -27
## [1] -30