0

I have >200000 rows of environmental data. some rows have data for temperature, others have data for turbidity and others have data for water flow.

Some measurements have been taken on the same day, at the same location.

How can I match my variables from different rows which share the same location and date?

Here is an example of my current data:

Sampling Point |  Date and Time  | Measurement | value | unit |

here is how I would like it:

Sampling point | Date and Time  | Temperature | Turbidity | Flow |
zx8754
  • 52,746
  • 12
  • 114
  • 209
Cos Z
  • 1
  • 1
    Check out how to write a [good example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). I think `merge()` function will solve your problem. It's very well desribed [here](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – Adamm Nov 19 '19 at 13:14
  • What you would do in Excel using `vlookup` is normally done using joins/merges. But it sounds like your data is already in one table and you need to reshape your data from long to wide instead - see eg https://seananderson.ca/2013/10/19/reshape/ for an explanation – anotherfred Nov 19 '19 at 13:16

1 Answers1

0

You can do this using tidyr's pivot_wider:

require(tidyr)
data %>% 
 pivot_wider(id_cols=c(Sampling.point, Date.and.Time),names_from=Measurement,values_from=c(values,unit))
iod
  • 7,412
  • 2
  • 17
  • 36