elena1234

Select data with subset or with $

Apr 6th, 2022 (edited)
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.46 KB | None | 0 0
  1. days <- c('Mon', 'Tue', 'Wed', 'Thur', 'Fri')
  2. temp <- c(20, 30, 20, 24, 25)
  3. rain <- c(T, F, F, F, T)
  4. df <- data.frame(days, temp, rain)
  5. print(df)
  6.  
  7. subset(df, subset = temp > 20)
  8.  
  9. # new.df <- df[df$temp > 20, ]
  10. # print(new.df)
  11.  
  12.  
  13. # Ex 10: What is the average mpg for cars that have more than 100 hp AND a wt value of more than 2.5.
  14. # avg <- mean(mtcars$mpg[mtcars$hp > 100 & mtcars$wt > 2.5])
  15. # avg <- mean(subset(mtcars, subset = hp > 100 & wt > 2.5)$mpg)
  16.  
Add Comment
Please, Sign In to add comment