Section 35 R Codes: Data Management
35.1 Select Columns
DF$Age
DF[, c('Age','Vac')]
DF[, c(2,4)]
DF[, -4]
DF[, -c(2,4)]
index <- which(names(DF) == 'Age')
index <- which(names(DF) %in% c('Age','Vac'))
35.2 Filter Rows
DF[DF$ID == 'S3',]
DF$Age[DF$ID == 'S25']
sum(DF$Age > 1.5)
sum(DF$Age > 1.5, na.rm = TRUE)
index <- which(DF$Age > 1.5)
DF[index,]
index <- DF$Sex == 'M' & !DF$Vac
DF[index,]
index <- DF$Sex == 'F' & DF$Vac
sum(index)
index <- DF$Age > 1.5 & DF$Vac
DF[index,]
35.3 Arrange Rows
index <- order(DF$Age, decreasing=FALSE)
DF[index,]
index <- order(DF$Wt, decreasing=TRUE)
DF[index,]
index <- order(DF$Age, DF$Wt, na.last=TRUE, decreasing=c(FALSE,TRUE), method='radix')
DF[index,]
index <- order(DF$Vac, decreasing = FALSE)
DF[index,]