R传播错误: Duplicate identifiers for rows

标签 r dplyr tidyr

使用数据集 df:

 df
 confint        row Index
 0.3407,0.4104    1     1
 0.2849,0.4413    2     2
 0.2137,0.2674    3     3
 0.1910,0.4575    4     1
 0.4039,0.4905    5     2
 0.403,0.4822     6     3
 0.0301,0.0646    7     1
 0.0377,0.0747    8     2
 0.0835,0.0918    9     3
 0.0437,0.0829   10     1
 0.0417,0.0711   11     2
 0.0718,0.0798   12     3
 0.0112,0.0417   13     1
 0.019,0.0237    14     2
 0.0213,0.0293   15     3
 0.0121,0.0393   16     1
 0.0126,0.0246   17     2
 0.0318,0.0428   18     3
 0.0298,0.0631   19     1
 0.018,0.0202    20     2
 0.1031,0.1207   21     3

这应该是一个相当容易从长格式转换为宽格式的数据集,即 7(行)x 3(列)数据帧。结果应包含 3 列(由 Index 命名)和 7 行 (21/3 = 7)。代码如下:

df <- spread(df,Index, confint, convert = FALSE)

但是,通过使用 Spread() 我收到以下错误:

Error: Duplicate identifiers for rows (1, 4, 7, 10, 13, 16, 19), (2, 5, 8, 11, 14, 17, 20), (3, 6, 9, 12, 15, 18, 21)

任何帮助将不胜感激!

最佳答案

我们需要创建一个序列列,然后展开

library(tidyverse)
df %>%
  group_by(Index) %>%
  mutate(ind = row_number()) %>%
  spread(Index, confint, convert = FALSE)

注意:这将是原始数据集中的问题,而不是帖子中显示的示例数据的问题

关于R传播错误: Duplicate identifiers for rows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54068141/

相关文章:

r - Shiny 的服务器。打印 JSON 作为结果输出

r - 将 .SD 与重命名的变量与 .SD 列的名称相结合

r - 我应该如何从 R data.table 中删除 NA block

r - 使用 dplyr::mutate() 创建新变量而不发生名称冲突

r - 使用R中的unite函数并删除重复值

r - x[floor(d)] + x[ceiling(d)] : non-numeric argument to binary operator 中的错误

r - 用日期的最大值在 dplyr 中汇总 - R

r - 与sparklyr一起使用时sample_n真的是随机样本吗?

以编程方式对数据帧重新排序,而不是对其进行子集化

r - 在带有变量的 2 行上使用 melt 或 pivot_longer