Section 19 Daframe Operation

19.1 Operation on data.frame

  • Be careful while carrying out matrix-like operation on the data.frame

  • Matrix-like operation on data.frame may not give intended outcomes; it may not even show warning and error

  • Always select the specific column in a data.frame as a vector and then do the vector-like operation as we have seen earlier


19.2 Mathematical operation

x <- c(1:10); y <- rep(c('M','F'), each=5); z <- rep(c(T,F), length=10)

DF <- data.frame(Age=x, Sex=y, Vac=z)

x <- DF$Age

x+5
x-5
x*5
x/2
x^2

sqrt(DF$Age)

log(DF$Age)


19.3 Statistical operation

x <- c(1:10); y <- rep(c('M','F'), each=5); z <- rep(c(T,F), length=10)

DF <- data.frame(Age=x, Sex=y, Vac=z)

x <- DF$Age

sum(x, na.rm = TRUE)
min(x, na.rm = TRUE)
max(x, na.rm = TRUE)
range(x, na.rm = TRUE)
mean(x, na.rm = TRUE)
median(x, na.rm = TRUE)
var(x, na.rm = TRUE)
sd(x, na.rm = TRUE)
sqrt(var(x, na.rm = TRUE))


19.4 Logical operation

x <- c(1:10); y <- rep(c('M','F'), each=5); z <- rep(c(T,F), length=10)

DF <- data.frame(Age=x, Sex=y, Vac=z)

x <- DF$Age

x > 5
x[x > 5]

x < 3
x[x < 5]

(3 < x) & (x < 7)
x[(3 < x) & (x < 7)]