
name<-c("Frank","Bob","Sally","Susan","Joan","Bill","Richard","Jane","Jill","John")
age<-c(34,28,19,28,30,47,24,34,32,64)
BMI<-c(24.2
,18.3
,15.4
,22.7
,29.2
,32.4
,21.0
,40.4
,24.8
,34.4
)
Factor_A= c(-1,-1,-1,-1,-1, 1, 1, 1, 1, 1)
Factor_B= c( 1,-1, 1,-1, 1,-1, 1,-1, 1,-1)
dat<-data.frame(name,age, BMI,Factor_A,Factor_B)
a. Without manually entering the data, create a new column Factor AB that is the product of Factor A and Factor B
dat$FactorAB<-dat$Factor_A*dat$Factor_B
b)Create a new column Factor C that looks as follows
(-1,-1,1,1,-1,-1,1,1,-1,-1)
dat$FactorC<-c(1,-1,1,1,-1,-1,1,1,-1,-1)
dat$FactorABC<-dat$Factor_A*dat$Factor_B*dat$FactorC
d)Make sure that all Factor columns are recognized as factors in R
dat$Factor_A<-as.factor(dat$Factor_A)
dat$Factor_B<-as.factor(dat$Factor_B)
dat$FactorC<-as.factor(dat$FactorC)
dat$FactorAB<-as.factor(dat$FactorAB)
dat$FactorABC<-as.factor(dat$FactorABC)

dat$smoking<-c("Yes","No","No","Yes","Yes","No","Yes","Yes","No","Yes")
dat$smoking<-as.factor(dat$smoking)
dat$BMI[dat$name=="Richard"]<-NA
dat$L_BMI<-log(dat$BMI)
5)Create a new dataframe dat2 selecting only the columns from dat corresponding to logarithm of BMI, Factor A, Factor B, and Factor AB
dat2<-data.frame(dat$L_BMI,dat$Factor_A,dat$Factor_B,dat$FactorAB)
dat3<-dat2[1:5,]
dat3
## dat.L_BMI dat.Factor_A dat.Factor_B dat.FactorAB
## 1 3.186353 -1 1 -1
## 2 2.906901 -1 -1 1
## 3 2.734368 -1 1 -1
## 4 3.122365 -1 -1 1
## 5 3.374169 -1 1 -1
## name age BMI Factor_A Factor_B FactorAB
## 1 Frank 34 24.2 -1 1 -1
## 2 Bob 28 18.3 -1 -1 1
## 3 Sally 19 15.4 -1 1 -1
## 4 Susan 28 22.7 -1 -1 1
## 5 Joan 30 29.2 -1 1 -1
## 6 Bill 47 32.4 1 -1 -1
## 7 Richard 24 21.0 1 1 1
## 8 Jane 34 40.4 1 -1 -1
## 9 Jill 32 24.8 1 1 1
## 10 John 64 34.4 1 -1 -1
## dat.L_BMI dat.Factor_A dat.Factor_B dat.FactorAB
## 1 3.186353 -1 1 -1
## 2 2.906901 -1 -1 1
## 3 2.734368 -1 1 -1
## 4 3.122365 -1 -1 1
## 5 3.374169 -1 1 -1
## 6 3.478158 1 -1 -1
## 7 NA 1 1 1
## 8 3.698830 1 -1 -1
## 9 3.210844 1 1 1
## 10 3.538057 1 -1 -1
## dat.L_BMI dat.Factor_A dat.Factor_B dat.FactorAB
## 1 3.186353 -1 1 -1
## 2 2.906901 -1 -1 1
## 3 2.734368 -1 1 -1
## 4 3.122365 -1 -1 1
## 5 3.374169 -1 1 -1