How would you write code in R studio for: Step 2: In a new code chunk below the
ID: 3879157 • Letter: H
Question
How would you write code in R studio for:
Step 2: In a new code chunk below the last, write code that will create a subset of the data containing just the name, height and weight columns for males that are no more than 48" tall (Southpark fitness data) or no more than 68" tall (your own lab 1 fitness data).
Step 3: Below the last code, write another command that will create a dataframe with just the name, time, sbp, dbp and xbp variables for females at least 9 years old (Southpark fitness data) or at least 19 years old (your own lab 1 fitness data).
Generic structure: newdataset <- subset(parentdataset, cases to retain, select=c(list of variables))
Some examples:
data.l.males <- subset(data.l, gender=="m") #Just the males.
data.l.a8 <- subset(data.l, age==8) #Just the 8 year olds.
data.l.h48 <- subset(data.l, height >= 48) #Just the subjects at least 48" tall ( >= means "greater than or equal to")
data.l.a9m <- subset(data.l, age==9 & gender=="f") #Just the 9 year old females
Explanation / Answer
Assuming the South park fitness data is in variable "data.I".
Step 2:
# This line code will fetch only the specified column i.e name, height and weight
data.i.col <- subset(data.I, select = c("name","height","weight", "gender"))
# This line of code will extract data created from the above step
data.i.male <- subset(data.i.col, height > 48 & height < 68 & gender == "male"))
Step 3:
# This line code will fetch only the specified column whose gender is female and age is atleast 9 years.
data.i.col <- subset(data.I, select = c("name","time","sbp", "dbp", "xbp") & gender == "female" & age >= 9)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.