【发布时间】:2017-06-03 23:41:12
【问题描述】:
我尝试在tidyverse包中使用gather和spread函数,但是它在spread函数中抛出错误
库(插入符号)
dataset<-iris
# gather function is to convert wide data to long data
dataset_gather<-dataset %>% tidyr::gather(key=Type,value = Values,1:4)
head(dataset_gather)
# spead is the opposite of gather
下面的代码会抛出类似这样的错误 Error: Duplicate identifiers for rows
dataset_spead<- dataset_gather%>%tidyr::spread(key = Type,value = Values)
【问题讨论】:
-
使用
gather后你没有唯一标识符,所以你不能在这样的数据帧上使用spread -
也许如果您包含一些您可以使用的实际数据以及您希望它看起来如何的示例,它会更有帮助
-
向宽格式添加行标识符,例如
iris %>% mutate(i = row_number()) %>% gather(var, val, -i) %>% spread(var, val) -
感谢@alistaire 的评论!
标签: r syntax-error spread