0

Table 1 I would like to collapse the rows so each timestamp is 1 row.

library(tidyverse)

table1 <- tribble(
  ~time, ~buoy, ~depth1, ~depth2, ~depth3,
  "t1",  "b1",  NA,     NA,     "value",
  "t1",  "b1",  NA,     "value", NA,
  "t1",  "b1",  "value", NA,     NA,
  "t2",  "b1",  NA,     NA,     "value",
  "t2",  "b1",  NA,     "value", NA,
  "t2",  "b1",  "value", NA,     NA
)

I currently have code to convert a long dataset with multiple parameters into a wide dataset with the timestamps as headers (see Table 2). I need the depths as headers and the time as rows in long format.

Table 2

table2 <- tribble(
  ~depth, ~buoy, ~time1, ~time2, ~timen, ...,
  "d1",   "b1",  "value", "value", "value", "v",
  "d2",   "b1",  "value", "value", "value", "v",
  "d3",   "b1",  "value", "value", "value", "v",
  "d4",   "b1",  "value", "value", "value", "v",
  "d2",   "b1",  "value", "value", "value", "v",
  "t2",   "b1",  "value", "value", "value", "v"
)
Mark
  • 7,785
  • 2
  • 14
  • 34
  • please share your data and expected output – Mike Jul 07 '23 at 19:33
  • 1
    Can you provide some representative data? Using that data, what should your desired output look like? – Melissa Key Jul 07 '23 at 19:33
  • Welcome to SO, Jade Hinson! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (you have a great start here), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jul 07 '23 at 19:34

1 Answers1

0

It sounds like you want something like this:

table1 %>%
  group_by(time, buoy) %>%
  summarise_all((list(na.omit))) %>%
  ungroup()

# A tibble: 2 × 5
  time  buoy  depth1 depth2 depth3
  <chr> <chr> <chr>  <chr>  <chr>
1 t1    b1    value  value  value
2 t2    b1    value  value  value
Mark
  • 7,785
  • 2
  • 14
  • 34