Guest User

Untitled

a guest
Jun 17th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. # Load data.table
  2. library(data.table)
  3. # Import food.csv: food
  4. dt_food<-fread("food.csv")
  5. # Convert food to a data frame
  6. df_food<-data.frame(dt_food)
  7. # View summary of food
  8. summary(food)
  9. # View head of food
  10. head(food)
  11. # View structure of food
  12. str(food)
  13. # Load dplyr
  14. library(dplyr)
  15. # View a glimpse of food
  16. glimpse(food)
  17. # View column names of food
  18. colnames(food)
  19.  
  20. # Define vector of duplicate cols
  21. duplicates <- c(4, 6, 11, 13, 15, 17, 18, 20, 22,
  22. 24, 25, 28, 32, 34, 36, 38, 40,
  23. 44, 46, 48, 51, 54, 65, 158)
  24. # Remove duplicates from food: food2
  25. food2<-food[-duplicates]
  26.  
  27. # Define useless vector
  28. useless <- c(1, 2, 3, 32:41)
  29. # Remove useless columns from food2: food3
  30. food3<-food2[-useless]
  31.  
  32. # Create vector of column indices: nutrition
  33. nutrition<-str_detect(names(food3),"100g")
  34. # View a summary of nutrition columns
  35. summary(food3[,nutrition])
  36.  
  37. # Find indices of sugar NA values: missing
  38. missing <- is.na(food3$sugars_100g)
  39.  
  40. # Replace NA values with 0
  41. food3$sugars_100g[missing] <- 0
  42.  
  43. # Create first histogram
  44.  
  45. hist(food3$sugars_100g,breaks=100)
  46. # Create food4
  47. i<-which(food3$sugars_100g==0)
  48. food4 <- food3[-i, ]
  49. # Create second histogram
  50. hist(food4$sugars_100g,breaks=100)
  51.  
  52. # Find entries containing "plasti": plastic
  53. plastic<-str_detect(food3$packaging,"plasti")
  54.  
  55. # Print the sum of plastic
  56. sum(plastic)
Add Comment
Please, Sign In to add comment