【问题标题】:Is there a function in R that will let me convert a dataset into "long format" but also merge columns?R中是否有一个函数可以让我将数据集转换为“长格式”但也可以合并列?
【发布时间】:2019-11-10 02:46:20
【问题描述】:

我有一个来自 Pokemon 统计数据的数据集,其中包含大量数字和分类数据。我的最终目标是创建一个模型或推荐系统,用户可以输入口袋妖怪列表,该模型会找到他们可能喜欢的类似口袋妖怪。目前数据集看起来像这样:

ID   Name    Type1    Type2   HP  ATK   DEF
001  Bulba.. Grass    Poison  45  49    49
ect...

我想将此数据转换为“长格式”,因为该格式对 R 中的许多其他函数更友好,但我在处理 Type1/Type2 列时遇到了麻烦。 有没有一种方法可以将这两者合并为一列(如“类型”),然后将数据转换为新格式?像这样的:

ID   Name    Type    Stat   Value
 001  Bulba.. Grass  HP     45
 001  Bulba.. Poison HP     45
 001  Bulba.. Grass  ATK    49
 001  Bulba.. Poison ATK    49

我知道对于双重类型的口袋妖怪,它会成为一个伪条目,但我没有看到任何更清洁的方法来实现这一点。我也知道使用dpylr的gather函数,但我只能使用这种方法真正完成Stat列,而不是Type问题。

谁能帮我弄清楚我如何才能做到这一点或知道其他更有效的方法吗?

【问题讨论】:

标签: r function wrangle


【解决方案1】:

1) pivot_longer 像这样重塑数据框两次:

library(dplyr)
library(tidyr)

DF %>%
  pivot_longer(starts_with("Type"), values_to = "Type") %>%
  select(-name) %>%
  pivot_longer(c("HP", "ATK", "DEF"), names_to = "Stat", values_to = "Value")

给予:

# A tibble: 6 x 5
  ID    Name    Type   Stat  Value
  <chr> <chr>   <chr>  <chr> <int>
1 001   Bulba.. Grass  HP       45
2 001   Bulba.. Grass  ATK      49
3 001   Bulba.. Grass  DEF      49
4 001   Bulba.. Poison HP       45
5 001   Bulba.. Poison ATK      49
6 001   Bulba.. Poison DEF      49

2) 融化 交替使用 data.table 中的melt 两次。

library(data.table)

m1 <- melt(DF, measure.var = grep("Type", names(DF)), value.name = "Type")
melt(m1, measure.var = c("HP", "ATK", "DEF"), 
  variable.name = "Stat", value.name = "Value")[-3]

给予:

   ID    Name   Type Stat Value
1 001 Bulba..  Grass   HP    45
2 001 Bulba.. Poison   HP    45
3 001 Bulba..  Grass  ATK    49
4 001 Bulba.. Poison  ATK    49
5 001 Bulba..  Grass  DEF    49
6 001 Bulba.. Poison  DEF    49

注意

DF 以可重现的形式假定为:

Lines <- "
ID   Name    Type1    Type2   HP  ATK   DEF
001  Bulba.. Grass    Poison  45  49    49"
DF <- read.table(text = Lines, header = TRUE, as.is = TRUE, 
  colClasses = list(ID = "character"))

【讨论】:

  • 每当我使用 pivot_longer() 时,我都会收到此“错误:由于名称错误而无法创建输出。* 使用names_repair 选择另一个策略调用rlang::last_error() 以查看回溯。”
  • 将注释中的代码复制并粘贴到新的 R 会话中,然后将代码复制并粘贴到答案中。您应该得到答案中显示的输出。
  • 我仍然无法让它在我的数据集上工作。它看起来像这样:pokedex_number name type1 type2 defense hp attack sp_attack sp_defense speed base_total &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; 1 Bulbasaur grass poison 49 45 49 65 65 45 318 我的代码是这样的:DF %&gt;% pivot_longer(starts_with("type"), values_to = "type") %&gt;% select(-name) %&gt;% pivot_longer(c("defense","hp","attack","sp_attack","sp_defense","speed","base_total"), names_to = "Stat", values_to = "Value")
  • 在问题中,该列被称为Name,但在您评论中的示例中,它被称为name,这会产生很大的不同,因为默认情况下pivot_longer没有names_to=参数会产生name 列,并且不能有两列具有相同的列名。将输入name 列重命名为Name(或使用pivot_longer 中的names_to 参数指定与name 默认值不同的列名称,并相应地更改select 语句)。
猜你喜欢
  • 2021-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-11
  • 2022-11-13
  • 2019-04-28
  • 1970-01-01
  • 2021-11-11
相关资源
最近更新 更多