Другой метод с использованием dplyr
и tidyr
:
library(dplyr)
library(tidyr)
df %>%
group_by(ID) %>%
arrange(DocDate = as.Date(DocDate)) %>%
unite("Price_Name", Price, Name, sep = "/") %>%
summarize(Price_Name = paste0("(", Price_Name, ")", collapse = ", "))
или просто:
df %>%
group_by(ID) %>%
arrange(DocDate = as.Date(DocDate)) %>%
summarize(Price_Name = paste0("(", Price, "/", Name, ")", collapse = ", "))
Выход:
# A tibble: 1 x 2
ID Price_Name
<int> <chr>
1 212 (3/def), (4/abc), (2/ghi), (5/jkl)
Данные:
df <- structure(list(ID = c(212L, 212L, 212L, 212L), DocDate = structure(c(4L,
2L, 1L, 3L), .Label = c("1/20/2017", "1/9/2017", "3/17/2017",
"3/6/2017"), class = "factor"), Price = c(4L, 3L, 2L, 5L), Name = structure(1:4, .Label = c("abc",
"def", "ghi", "jkl"), class = "factor")), .Names = c("ID", "DocDate",
"Price", "Name"), class = "data.frame", row.names = c(NA, -4L
))