Problem using spread()

Asked

Viewed 25 times

1

Hi, how are you? I’m having trouble using the function spread(): it shows error and does not pop.

dados<-structure(list(V1 = structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), class = "Date"), 
    EST = c("EST01", "EST01", "EST01", "EST01", "EST01", "EST01", 
    "EST01", "EST01", "EST01", "EST01", "EST01", "EST01", "EST01", 
    "EST01", "EST01", "EST01", "EST01", "EST01", "EST01", "EST01", 
    "EST01", "EST01", "EST01", "EST01", "EST01", "EST01", "EST01", 
    "EST01", "EST01", "EST01", "EST01", "EST01", "EST01", "EST01", 
    "EST01", "EST01", "EST01", "EST01", "EST01", "EST01", "EST01", 
    "EST01", "EST01", "EST01", "EST01", "EST01", "EST01", "EST01", 
    "EST02", "EST02"), TT = new("Period", .Data = c(0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), year = c(0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0), month = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0), day = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), hour = c(0, 
    0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 
    10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 
    17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 0, 0), 
        minute = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), 
    variable = c("SensorA", "SensorB", "SensorA", "SensorB", 
    "SensorA", "SensorB", "SensorA", "SensorB", "SensorA", "SensorB", 
    "SensorA", "SensorB", "SensorA", "SensorB", "SensorA", "SensorB", 
    "SensorA", "SensorB", "SensorA", "SensorB", "SensorA", "SensorB", 
    "SensorA", "SensorB", "SensorA", "SensorB", "SensorA", "SensorB", 
    "SensorA", "SensorB", "SensorA", "SensorB", "SensorA", "SensorB", 
    "SensorA", "SensorB", "SensorA", "SensorB", "SensorA", "SensorB", 
    "SensorA", "SensorB", "SensorA", "SensorB", "SensorA", "SensorB", 
    "SensorA", "SensorB", "SensorA", "SensorB"), valor = c(0.013, 
    -0.073, 0.018, -0.125, 0.022, -0.169, 0.019, -0.154, 0.019, 
    -0.144, 0.021, -0.154, 0.021, -0.186, 0.021, -0.181, 0.021, 
    -0.203, 0.021, -0.189, 0.023, -0.185, 0.027, -0.211, 0.049, 
    -0.196, 0.08, -0.048, 0.049, 0.17, 0.026, 0.223, 0.036, 0.312, 
    0.045, 0.386, 0.041, 0.431, 0.031, 0.424, 0.022, 0.389, 0.028, 
    0.308, 0.042, 0.156, 0.021, 0.078, 0.013, -0.073)), class = "data.frame", row.names = c(NA, 
-50L))

when I try to use:

dados%>%spread(variable,valor)

he of error:

Error: Each Row of output must be identified by a Unique Combination of Keys. Keys are Shared for 48 Rows:

  • 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47
  • 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48

Run rlang::last_error() to see Where the error occurred.

I needed him to do what this function is doing, namely to unpack: (I know that the variable column was duplicated and so he added. x and . y)

I understand that there is duplication of information because it is only popping by the variable column, in this case how to paste more columns, see that in the function merge() for two subset() i used more reference columns.

merge(subset(dados%>%rename(`SensorA`=valor),variable=="SensorA"),subset(dados%>%rename(`SensorB`=valor),variable=="SensorB"), by=c("V1","EST","TT"))

1 answer

3


The function spread is notoriously difficult to use. Hence the package tidyr now recommends pivot_wider. Of help("spread"):

Development on spread() is complete, and for new code we recommend switching to pivot_wider(), which is easier to use, more featureful, and still under active Development. df %>% spread(key, value) is equivalent to df %>% pivot_wider(names_from = key, values_from = value)

See more Details in Vignette("pivot").

Google translation, edited, with my emphasis:

Spread() development is complete and, for the new code, we recommend switching to pivot_wider(), which is easier to use, with more resources and still in active development. df%>% spread(key, value) is equivalent to df%>% pivot_wider(names_from = key, values_from = value)

See more details on Vignette("pivot").

library(dplyr)
library(tidyr)

dados %>%
  pivot_wider(
    id_cols = -c(variable, valor),
    names_from = variable,
    values_from = valor
  )
## A tibble: 25 x 5
#   V1         EST   TT       SensorA SensorB
#   <date>     <chr> <Period>   <dbl>   <dbl>
# 1 1970-01-02 EST01 0S         0.013  -0.073
# 2 1970-01-02 EST01 1H 0M 0S   0.018  -0.125
# 3 1970-01-02 EST01 2H 0M 0S   0.022  -0.169
# 4 1970-01-02 EST01 3H 0M 0S   0.019  -0.154
# 5 1970-01-02 EST01 4H 0M 0S   0.019  -0.144
# 6 1970-01-02 EST01 5H 0M 0S   0.021  -0.154
# 7 1970-01-02 EST01 6H 0M 0S   0.021  -0.186
# 8 1970-01-02 EST01 7H 0M 0S   0.021  -0.181
# 9 1970-01-02 EST01 8H 0M 0S   0.021  -0.203
#10 1970-01-02 EST01 9H 0M 0S   0.021  -0.189
## … with 15 more rows

Browser other questions tagged

You are not signed in. Login or sign up in order to post.