简体   繁体   English

如何将此 dataframe(超过 2 行)转换为 R 中的事务?

[英]How can I transform this dataframe (with more than 2 rows) to a transaction in R?

I have a dataframe that contains:我有一个 dataframe 包含:

userID song   sex
1      songA  M
2      songB  F
1      songC  M
2      songA  F 
...    ...    ...

So each line is a register of a song listened by the user.所以每一行都是用户收听的歌曲的寄存器。 I want to use "arules" but first I need to transform this dataframe to a transaction.我想使用“arules”,但首先我需要将此 dataframe 转换为事务。 I've searched a lot but actually I'don't know if my idea is wrong because I have no answer yet.我搜索了很多,但实际上我不知道我的想法是否错误,因为我还没有答案。 I've find solutions like using split to create lists of lists with all songs listend by each user, but if I do that I'll lose the sex information.我找到了解决方案,例如使用 split 来创建包含每个用户收听的所有歌曲的列表列表,但如果我这样做,我会丢失性别信息。 I'll only get rules like {songA,songB} -> {songZ} .我只会得到像{songA,songB} -> {songZ}这样的规则。 I want to generate rules like {songA,songC,M} -> {songZ} (using the sex information).我想生成像{songA,songC,M} -> {songZ}这样的规则(使用性别信息)。 I don't know if I am wrong with my idea and this is not possible.我不知道我的想法是否有误,这是不可能的。 Any idea?任何想法?

Thanks.谢谢。

If you're looking at associations, you'll generally want to reshape your data into a long dataframe, with an ID column, and another column for your binary item attributes.如果您正在查看关联,您通常希望将您的数据重塑为一个长 dataframe,其中包含一个 ID 列和另一列用于您的二进制项目属性。

There are many ways to reshape your data to get the right form.有很多方法可以重塑数据以获得正确的形式。 In your example, I reshaped using tidyverse , and also added a distinct so that the user's gender wouldn't be stated multiple times.在您的示例中,我使用tidyverse进行了重塑,并且还添加了 distinct 以便用户的性别不会被多次声明。

txt = "
userID song   sex
1      songA  M
2      songB  F
1      songC  M
2      songA  F "
df <- read.table(text = txt, header = TRUE)

library(tidyverse)
df %>%
  pivot_longer(cols = c(song, sex)) %>%
  distinct()
#> # A tibble: 6 x 3
#>   userID name  value
#>    <int> <chr> <fct>
#> 1      1 song  songA
#> 2      1 sex   M    
#> 3      2 song  songB
#> 4      2 sex   F    
#> 5      1 song  songC
#> 6      2 song  songA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM