Course Notes Home

Solution 1

num<-16
if(num > 10){
  num<-10
}
print(num)
## [1] 10
## add in censor for -10 

num<- -25
if(num > 10){
  num<-10
} else {
  if(num < -10){
    num<- -10
  }
}
print(num)
## [1] -10
## use for loop to test all 4 numbers

test<-c(5,-9,16,-25)
for(num in test){
  if(num > 10){
    num<-10
  } else {
    if(num < -10){
      num<- -10
    }
  }
  print(num)
}
## [1] 5
## [1] -9
## [1] 10
## [1] -10

Solution 2

filenames <- list.files(path = "data", pattern = "inflammation.*csv", full.names = TRUE)
filename_max <- "" # filename where the maximum average inflammation patient is found
patient_max <- 0 # index (row number) for this patient in this file
average_inf_max <- 0 # value of the average inflammation score for this patient
for (f in filenames) {
  dat <- read.csv(file = f, header = FALSE)
  dat.means = apply(dat, 1, mean)
  for (patient_index in length(dat.means)){
    patient_average_inf = dat.means[patient_index]
    if (patient_average_inf average_inf_max) {
      average_inf_max = patient_average_inf
      filename_max <- f
      patient_max <- patient_index
      }
    }
  }
print(filename_max)
print(patient_max)
print(average_inf_max)

Solution 3

par(mfrow = c(3,4))
par(mar = c(4,4,4,4))
for(i in 7:13){
  hist(dat[,i], main = colnames(dat)[i], xlab = "Score")
}

Solution 4

par(mfrow = c(1,2))
par(mar = c(4,4,4,1))
x_range<-range(dat$Age)
plot(density(dat$Age[which(dat$Sex == "F")]), col = "purple", xlim = x_range, main = "Females", xlab = "Age")
plot(density(dat$Age[which(dat$Sex == "M")]), col = "green", xlim = x_range, main = "Males", xlab = "Age")

Solution 5

pVal<-rep(NA, 7)
for(colNum in 7:13){
  tOut<-t.test(dat[,colNum] ~ dat$CaseControl, alternative = "two.sided")
  pVal[colNum-6]<-tOut$p.value
  if(tOut$p.value < 0.05){
    boxplot(dat[,colNum] ~ dat$CaseControl, main = colnames(dat)[i], ylab = "Score")
    comment<-paste("P = ", signif(tOut$p.value,2), sep = "")
    mtext(comment, line = 0.5, side = 3, adj = 1)
  }
}

print(pVal)
## [1] 0.195266772 0.261189480 0.003824978 0.897704780 0.837231269 0.993929740
## [7] 0.903176985