tidyverse - gather and spread function in R -
i have created data frame using below code:
stocks <- data.frame(time = as.date('2009-01-01') + 0:9, x = rnorm(50, 20, 1), y= rnorm(50, 20, 2),= rnorm(50, 20, 2), z=rnorm(50,20,4)) )
i have applied gather function data frame:
res<-stocks%<%gather(company, value,-time)
while trying spread res getting error:
spread(data=res, key=company , value = value) error: duplicate identifiers rows
we need sequence column avoid error duplicate identifiers...
stocks %>% gather(company, value,-time) %>% group_by(company) %>% mutate(i = row_number()) %>% spread(company, value)
Comments
Post a Comment