【问题标题】:Separating a column in R [duplicate]在R中分离一列[重复]
【发布时间】:2016-05-10 08:37:37
【问题描述】:

我有一些简单的数据,比如下面的 MovieLense 1M 数据文件

  item_id                              title                       genres
1       1                   Toy Story (1995)  Animation|Children's|Comedy
2       2                     Jumanji (1995) Adventure|Children's|Fantasy
3       3            Grumpier Old Men (1995)               Comedy|Romance
4       4           Waiting to Exhale (1995)                 Comedy|Drama
5       5 Father of the Bride Part II (1995)                       Comedy
6       6                        Heat (1995)        Action|Crime|Thriller

我的genres 列数据包含 19 个值。我应该如何将我的数据更改为如上示例所示?

类型表

genreTbl['title']
         title
1      unknown
2       Action
3    Adventure
4    Animation
5   Children's
6       Comedy
7        Crime
8  Documentary
9        Drama
10     Fantasy
11   Film-Noir
12      Horror
13     Musical
14     Mystery
15     Romance
16      Sci-Fi
17    Thriller
18         War
19     Western

我想把我的数据改成这个结构:

  item_id                                          movie_title release_date
1       1                                     Toy Story (1995)         <NA>
2       2                                     GoldenEye (1995)         <NA>
3       3                                    Four Rooms (1995)         <NA>
4       4                                    Get Shorty (1995)         <NA>
5       5                                       Copycat (1995)         <NA>
6       6 Shanghai Triad (Yao a yao yao dao waipo qiao) (1995)         <NA>
  unknown Action Adventure Animation Children's Comedy Crime Documentary Drama
1       0      0         0         1          1      1     0           0     0
2       0      1         1         0          0      0     0           0     0
3       0      0         0         0          0      0     0           0     0
4       0      1         0         0          0      1     0           0     1
5       0      0         0         0          0      0     1           0     1
6       0      0         0         0          0      0     0           0     1
  Fantasy Film-Noir Horror Musical Mystery Romance Sci-Fi Thriller War Western
1       0         0      0       0       0       0      0        0   0       0
2       0         0      0       0       0       0      0        1   0       0
3       0         0      0       0       0       0      0        1   0       0
4       0         0      0       0       0       0      0        0   0       0
5       0         0      0       0       0       0      0        1   0       0
6       0         0      0       0       0       0      0        0   0       0

我需要我所有的流派都像上面一样在列中,如果我的项目流派值包含选定的流派值应该是 1 否则 0。

【问题讨论】:

  • 显示您尝试自己解决此问题的代码在哪里?
  • 请在写作中使用标点符号。
  • @RichardScriven 我不知道如何解决这个问题:(
  • 提示 - 编写一个函数,为“genrestring”返回 0/1 向量并将其应用于您的表。我可以帮你解决这个问题,但不是现在。
  • 当您在 Google 上搜索标题“在 R 中分离一列”时发生了什么?请参观并阅读about SO:“包括有关您尝试过的内容的详细信息”; “不要问...... [q]你没有试图找到答案的问题(展示你的作品!)”。

标签: r dataframe


【解决方案1】:

您也可以使用concat.split 函数,同样来自splitstackshape 包:

library(splitstackshape)
concat.split.expanded(df, split.col = "genres", sep = "|", type = "character",
                  mode = "binary", fixed = TRUE, fill = 0)

## Alternative alias
## Note also `drop = TRUE` to drop the original column
cSplit_e(mydf, split.col = "genres", sep = "|", type = "character", 
         mode = "binary", fixed = TRUE, fill = 0, drop = TRUE)

【讨论】:

    【解决方案2】:

    使用 splitstackshape 中的 cSplitreshape2 / data.table 中的 dcast 的组合。通过使用length 作为聚合函数,您可以创建逻辑整数值:

    library(splitstackshape)
    library(reshape2)   # or library(data.table)
    dcast(cSplit(mydf, "genres", sep="|", "long"),
          item_id + title ~ genres, 
          fun.aggregate = length)
    

    给出:

       item_id                        title Action Adventure Animation Children's Comedy Crime Drama Fantasy Romance Thriller
    1:       1               ToyStory(1995)      0         0         1          1      1     0     0       0       0        0
    2:       2                Jumanji(1995)      0         1         0          1      0     0     0       1       0        0
    3:       3         GrumpierOldMen(1995)      0         0         0          0      1     0     0       0       1        0
    4:       4        WaitingtoExhale(1995)      0         0         0          0      1     0     1       0       0        0
    5:       5 FatheroftheBridePartII(1995)      0         0         0          0      1     0     0       0       0        0
    6:       6                   Heat(1995)      1         0         0          0      0     1     0       0       0        1
    

    使用过的数据:

    mydf <- structure(list(item_id = 1:6, title = structure(c(5L, 4L, 2L, 
    6L, 1L, 3L), .Label = c("FatheroftheBridePartII(1995)", "GrumpierOldMen(1995)", 
    "Heat(1995)", "Jumanji(1995)", "ToyStory(1995)", "WaitingtoExhale(1995)"
    ), class = "factor"), genres = structure(c(3L, 2L, 6L, 5L, 4L, 
    1L), .Label = c("Action|Crime|Thriller", "Adventure|Children's|Fantasy", 
    "Animation|Children's|Comedy", "Comedy", "Comedy|Drama", "Comedy|Romance"
    ), class = "factor")), .Names = c("item_id", "title", "genres"
    ), class = "data.frame", row.names = c("1", "2", "3", "4", "5", 
    "6"))
    

    【讨论】:

    • 感谢您的帮助,您的代码和示例数据非常完美,我将在我的项目中对其进行测试 :) 谢谢您数十亿的时间。您能否提供一个很好的参考来学习 R 作为自己的大师?
    • 在测试您的示例代码后,我丢失了 item_id ?我该如何解决这个问题,在您的示例代码中也没有 item_id。
    • @WhizDeveloper 查看更新后的答案,HTH
    • 感谢您的快速回答,您太有才了
    • @WhizDeveloper thanx,请参阅info page of the R-tag 了解有关 R 的资源列表
    猜你喜欢
    • 1970-01-01
    • 2015-10-31
    • 2018-09-19
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 2021-08-06
    相关资源
    最近更新 更多