【问题标题】:R tidyr spread Error: Duplicate identifiers for rows [duplicate]R tidyr spread错误:行的标识符重复[重复]
【发布时间】:2018-10-18 22:16:04
【问题描述】:

我遇到了 R 上的 tidyr::spread() 函数的问题。

之前我运行了 melt() 函数来删除 NAs 值并缩小我的数据。

    `NPP0 <- melt(NPP, variable.names("3", "13", "14", "15", "16", "24", "25", "26"), na.rm=T)`

它运行良好.. 并在名为“变量”的列上产生了我的“variable.names”,如上所述,以及一个具有相应值的值列。

    variable   value
2           3 2688.00
3           3 1432.00
4           13 1336.00
5           14 1152.00
8           .. 1832.00

现在我想返回并将每个变量按一列分组,对应于其分类名称。

Just checking..
str(NPP0)
'data.frame':   5783 obs. of  2 variables:
 $ variable: Factor w/ 8 levels "3","13","14",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value   : num  2688 1432 1336 1152 1832 ...

Then:

    NPP1 <-  spread(NPP0, key='variable', value='value', convert = T)

Gives:

    Error: Duplicate identifiers for rows (1, 2, 3,...)

我也尝试了 reshape2::dcast() 函数。虽然它给出了一些非常奇怪的东西:

    NPP1 <- dcast(NPP0, value ~ variable, value.var = 'value')

Aggregation function missing: defaulting to length

       value  3 13 14 15 16 24 25 26
1       0.16  0  0  0  0  0  1  0  0
2       0.92  0  7  0  0  0  0  0  0
3       1.00  0  2  0  0  0  0  0  0

Can anyone help with this?

【问题讨论】:

  • 你希望你的输出到底是什么样的?

标签: r tidyr reshape2 spread dcast


【解决方案1】:

我用这个解决了:

# Removing NA values #
NPP0 <- melt(NPP, variable.names("3", "13", "14", "15", "16", "24", "25",26"), na.rm=T)

library(tidyr)

NPP1 <- as.data.frame (NPP0 %>% 
  group_by(variable) %>% 
  mutate(id = row_number()) %>% 
  spread(variable, value) )

Which gives:
View(NPP1)
[Reulting dataframe][1]

  [1]: https://i.stack.imgur.com/kI1HD.png

tHANK you for helping..

【讨论】:

    【解决方案2】:

    您的数据没有任何行标识符。可能是这个原因。

    NPP0$samples<-rownames(NPP0)
    NPP1 <-  spread(NPP0, key='variable', value='value', fill=0)
    

    试试吧,我希望它有效。

    【讨论】:

    • 嗨,BJK,我解决了这个问题:NPP0 % group_by(variable) %>% mutate(id = row_number()) %>% spread(variable, value) %>% select(-id) )