我正在尝试根据ID列中的ID值组合下面的Item.ID列中的值,Item.ID列中的值可以使用逗号分隔:
数据框(AS):
fig = Figure(figsize=(5, 4), dpi=100)
# A canvas must be manually attached to the figure (pyplot would automatically
# do it). This is done by instantiating the canvas with the figure as
# argument.
canvas = FigureCanvasAgg(fig)
# your plotting here
canvas.draw()
s, (width, height) = canvas.print_to_buffer()
# Option 2a: Convert to a NumPy array.
X = np.fromstring(s, np.uint8).reshape((height, width, 4))
必需的输出
AS <- data.frame("Index" = c(1,1,2,2,2,3,4), "Item.ID" = c("A1","C2","A3","U4","M5","K6","Y9"))
答案 0 :(得分:2)
使用仅带有aggregate
和paste
的基数R:
AS.Wide <- aggregate(AS$Item.ID, by=list(Index=AS$Index), paste, collapse=",")
如果要保留“ Item.ID”变量名称,则需要对其进行更改:
names(AS.Wide)[2] <- "Item.ID"
答案 1 :(得分:1)
使用Tidyverse / dplyr:
按索引分组
还要按Item.ID排列它们,以便结果按字母顺序排列,但这取决于您。
将所有Item.ID粘贴在一起,并用“,”折叠。
library(dplyr)
AS_Wide <- AS %>%
group_by(Index) %>%
arrange(Item.ID) %>%
summarize(Item.ID = paste(Item.ID, collapse = ","))