r语言按照一列进行分组转置
数据样式:
group value
A 1
A 2
A 3
B 5
B 6
C 7
代码:
library(readxl)
library(arules)
df <- read_xlsx('E:/data.xlsx',sheet = "Sheet1")
df_1 <- as(split(df$group, df$value),"transactions")
#转成数据框类型
df_2 <- as(df_1, "data.frame")
#去掉大括号
library(stringr)
df_2$items <- str_sub(df_2$items, start = 2L, end = str_length(df_2$items) - 1)
结果样式:
group value
A 1,2,3
B 5,6
C 7