[英]How to replace reshape2::melt for an array with tidyr?
I would like to convert a matrix/array (with dimnames) into a data frame.我想将矩阵/数组(带有dimnames)转换为数据框。 This can be done very easily using
reshape2::melt
but seems harder with tidyr
, and in fact not really possible in the case of an array.这可以很容易用做
reshape2::melt
但似乎更难tidyr
,而事实上不是真的有可能在阵的情况。 Am I missing something?我错过了什么吗? (In particular since
reshape2
describes itself as being retired; see https://github.com/hadley/reshape ). (特别是因为
reshape2
将自己描述为已退休;请参阅https://github.com/hadley/reshape )。
For example, given the following matrix例如,给定以下矩阵
MyScores <- matrix(runif(2*3), nrow = 2, ncol = 3,
dimnames = list(Month = month.name[1:2], Class = LETTERS[1:3]))
we can turn it into a data frame as follows我们可以把它变成一个数据框,如下所示
reshape2::melt(MyScores, value.name = 'Score') # perfect
or, using tidyr
as follows:或者,使用
tidyr
如下:
as_tibble(MyScores, rownames = 'Month') %>%
gather(Class, Score, -Month)
In this case reshape2
and tidyr
seem similar (although reshape2
is shorter if you are looking for a long-format data frame).在这种情况下,
reshape2
和tidyr
看起来很相似(尽管如果您正在寻找长格式的数据框, reshape2
会更短)。
However for arrays, it seems harder.但是对于数组,似乎更难。 Given
给定的
EverybodyScores <- array(runif(2*3*5), dim = c(2,3,5),
dimnames = list(Month = month.name[1:2], Class = LETTERS[1:3], StudentID = 1:5))
we can turn it into a data frame as follows:我们可以将其转换为数据框,如下所示:
reshape2::melt(EverybodyScores, value.name = 'Score') # perfect
but using tidyr
it's not clear how to do it:但使用
tidyr
不清楚如何做到这一点:
as_tibble(EverybodyScores, rownames = 'Month') # looses month information and need to distange Class and StudentID
Is this a situation where the right solution is to stick to using reshape2
?在这种情况下,正确的解决方案是坚持使用
reshape2
吗?
One way I just found by playing around is to coerce via tbl_cube
.我刚刚通过玩耍发现的一种方法是通过
tbl_cube
进行强制。 I have never really used the class but it seems to do the trick in this instance.我从来没有真正使用过这个类,但在这种情况下它似乎可以解决问题。
EverybodyScores <- array(
runif(2 * 3 * 5),
dim = c(2, 3, 5),
dimnames = list(Month = month.name[1:2], Class = LETTERS[1:3], StudentID = 1:5)
)
library(tidyverse)
library(cubelyr)
EverybodyScores %>%
as.tbl_cube(met_name = "Score") %>%
as_tibble
#> # A tibble: 30 x 4
#> Month Class StudentID Score
#> <chr> <chr> <int> <dbl>
#> 1 January A 1 0.366
#> 2 February A 1 0.254
#> 3 January B 1 0.441
#> 4 February B 1 0.562
#> 5 January C 1 0.313
#> 6 February C 1 0.192
#> 7 January A 2 0.799
#> 8 February A 2 0.277
#> 9 January B 2 0.631
#> 10 February B 2 0.101
#> # ... with 20 more rows
Created on 2018-08-15 by the reprex package (v0.2.0).由reprex 包(v0.2.0) 于 2018 年 8 月 15 日创建。
Making a tibble drops the row names, but instead of going straight into a tibble, you can make the array into a base R data.frame
, then use tidyr::rownames_to_column
to make a column for months.制作 tibble 会删除行名称,但不是直接进入 tibble,您可以将数组制作为基本 R
data.frame
,然后使用tidyr::rownames_to_column
制作一列数月。 Notice that converting to a data frame creates columns with names like A.1
, sticking the class and ID together;请注意,转换为数据框会创建名称类似于
A.1
列,将类和 ID 粘在一起; you can separate these again with tidyr::separate
.您可以使用
tidyr::separate
再次将它们tidyr::separate
。 Calling as_tibble
is optional, just for if you care about it being a tibble
in the end, and also can come at any point in the workflow once you've made a column from the row names.调用
as_tibble
是可选的,只是因为您是否关心它最终是一个tibble
,并且一旦您从行名称中创建了一个列,也可以在工作流程中的任何点调用。
library(tidyverse)
EverybodyScores <- array(runif(2*3*5), dim = c(2,3,5),
dimnames = list(Month = month.name[1:2], Class = LETTERS[1:3], StudentID = 1:5))
EverybodyScores %>%
as.data.frame() %>%
rownames_to_column("Month") %>%
gather(key = class_id, value = value, -Month) %>%
separate(class_id, into = c("Class", "StudentID"), sep = "\\.") %>%
as_tibble()
#> # A tibble: 30 x 4
#> Month Class StudentID value
#> <chr> <chr> <chr> <dbl>
#> 1 January A 1 0.576
#> 2 February A 1 0.229
#> 3 January B 1 0.930
#> 4 February B 1 0.547
#> 5 January C 1 0.761
#> 6 February C 1 0.468
#> 7 January A 2 0.631
#> 8 February A 2 0.893
#> 9 January B 2 0.638
#> 10 February B 2 0.735
#> # ... with 20 more rows
Created on 2018-08-15 by the reprex package (v0.2.0).由reprex 包(v0.2.0) 于 2018 年 8 月 15 日创建。
Here is the new tidyr
way to do the same:这是执行相同操作的新
tidyr
方法:
library(tidyr)
EverybodyScores <- array(
runif(2 * 3 * 5),
dim = c(2, 3, 5),
dimnames = list(Month = month.name[1:2], Class = LETTERS[1:3], StudentID = 1:5)
)
as_tibble(EverybodyScores, rownames = "Month") %>%
pivot_longer(
cols = matches("^A|^B|^C"),
names_sep = "\\.",
names_to = c("Class", "StudentID")
)
#> # A tibble: 30 x 4
#> Month Class StudentID value
#> <chr> <chr> <chr> <dbl>
#> 1 January A 1 0.0325
#> 2 January B 1 0.959
#> 3 January C 1 0.593
#> 4 January A 2 0.0702
#> 5 January B 2 0.882
#> 6 January C 2 0.918
#> 7 January A 3 0.459
#> 8 January B 3 0.849
#> 9 January C 3 0.901
#> 10 January A 4 0.328
#> # … with 20 more rows
Created on 2021-02-23 by the reprex package (v1.0.0)由reprex 包(v1.0.0) 于2021年 2 月 23 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.