如何 pivot/merge R 中的数据

How to pivot/merge data in R

这是我的数据集的示例

可重现的数据帧

datt <- structure(
  list(
    Name = c("John", "John", "John", "John"),
    Type = c("a",
             "a", "b", "b"),
    Distance = c(50, 100, 50, 100),
    Value = c(2,
              4, 3, 6),
    Peak = c(30, 30, 45, 45)
  ),
  class = c("spec_tbl_df",
            "tbl_df", "tbl", "data.frame"),
  row.names = c(NA, -4L),
  spec = structure(list(
    cols = list(
      Name = structure(list(), class = c("collector_character",
                                         "collector")),
      Type = structure(list(), class = c("collector_character",
                                         "collector")),
      Distance = structure(list(), class = c("collector_double",
                                             "collector")),
      Value = structure(list(), class = c("collector_double",
                                          "collector")),
      Peak = structure(list(), class = c("collector_double",
                                         "collector"))
    ),
    default = structure(list(), class = c("collector_guess",
                                          "collector")),
    skip = 1
  ), class = "col_spec")
)

这是我想要看到的结构

使用R,谁能指出正确的方向?我想我需要合并 Peak 和 Value,并以某种方式在其中设置一个支点。

你可以试试这个:

library(dplyr)
library(tidyr)

datt %>%
  select(Name, Type, Peak) %>%
  pivot_longer(cols = Peak, names_to = 'Distance', values_to = 'Value') %>%
  distinct() %>%
  bind_rows(datt %>% 
              mutate(Distance = as.character(Distance)) %>%
              select(-Peak)) %>%
  arrange(Name, Type)

#  Name  Type  Distance Value
#  <chr> <chr> <chr>    <dbl>
#1 John  a     Peak        30
#2 John  a     50           2
#3 John  a     100          4
#4 John  b     Peak        45
#5 John  b     50           3
#6 John  b     100          6

但是,请注意 Distance 列现在是字符类型,因为数据框中只能有一种类型的列。

这是data.table选项

setDT(datt)[,
  rbind(setNames(data.frame("Peak", unique(Peak)), names(.SD)), .SD),
  .(Name, Type),
  .SDcols = c("Distance", "Value")
]

这给出了

   Name Type Distance Value
1: John    a     Peak    30
2: John    a       50     2
3: John    a      100     4
4: John    b     Peak    45
5: John    b       50     3
6: John    b      100     6