Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. - **2e.** Check that in both `sprint.m.df` and `sprint.w.df`, the values in `City.Date` column
  2. only once (i.e., there are no duplicated values).
  3. Do this in a way that you find suitable, but when you Knit this Lab,
  4. the results that demonstrate this claim should be visible in the HTML file.
  5.  
  6. ```{r}
  7.  
  8. duplicate.bool = FALSE
  9.  
  10. for (i in (length(sprint.m.df["Date"]) - 1)) {
  11.  
  12. if (sprint.m.df["Date"][i] == sprint.m.df["Date"][i + 1]) {
  13.  
  14. duplicate.bool = TRUE
  15.  
  16. }
  17. }
  18.  
  19. duplicate.bool
  20.  
  21. ```
  22.  
  23. Merging data
  24. ===
  25.  
  26. - **3a.** In preparation of merging `sprint.m.df` and `sprint.w.df`, we first
  27. want to find all the sprints that occur in the same race in both data frames.
  28. Specifically, remove all the rows in `sprint.m.df` that have a `City.Date`
  29. that does not occur in `sprint.w.df`. Likewise, remove all the rows in
  30. `sprint.w.df` that have a `City.Date`
  31. that does not occur in `sprint.m.df`.
  32. Then, remove the `City` and `Date` columns in both data frames.
  33. (Hint: You might be interested in the `%in%` function in R. Try looking this up
  34. to see what it does.)
  35. In the end, both `sprint.m.df` and `sprint.w.df` should have 385 rows and 7 columns.
  36. Print out the first 3 lines of `sprint.m.df` and `sprint.w.df`
  37. afterwards.
  38.  
  39. ```{r}
  40.  
  41. in.both = vector(mode="numeric", length=0)
  42.  
  43. for (i in (length(sprint.m.df["City.Date"]))) {
  44. temp = which(sprint.w.df$City.Date == sprint.m.df["City.Date"][i])
  45.  
  46. in.both = in.both + temp
  47.  
  48. }
  49.  
  50. sprint.w.df = sprint.w.df[-in.both]
  51.  
  52. head(sprint.m.df, n = 3)
  53. head(sprint.w.df, n = 3)
  54.  
  55. ```
  56.  
  57. - **3b.** We now will complete the manual merge of `sprint.m.df` and `sprint.w.df`.
  58. Here are the sequence steps to do: First, check the order of values in `City.Date` in
  59. `sprint.m.df` match exactly with those in `sprint.w.df`. Second, use the `cbind()`
  60. function appropriately to create a new data frame `sprint.df` that has 13 columns.
  61. The first column should be `City.Date`, the next 6 columns should contain all the
  62. remaining columns from `sprint.m.df`, and the last 6 columns should contain all the
  63. remaining columns form `sprint.w.df`. Of course, each row should correspond to
  64. sprints from the same `City.Date`. Print out the first 3 lines of `sprint.df`
  65. afterwards.
  66.  
  67. ```{r}
  68.  
  69. idential(sprint.m.df["City.Date"], sprint.w.df["City.Date"])
  70.  
  71. sprint.df = cbind(sprint.m.df, sprint.w.df, by = "City.Date")
  72.  
  73. head(sprint.df, n = 3)
  74.  
  75. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement