“Нет человека и нет проблем”

Анатолий Рыбаков

“No women no cry”

Bob Marley

Bližnjica do grafa v naslovu je TUKAJ

WIDE DATA FORMAT TO LONG DATA FORMAT

Python

Glej TUKAJ

Jupiter Notebook list je na voljo TUKAJ

Excel

Izvedem s pomočjo Power Query. Navodila so v dokumentu TUKAJ

R

##wide to long
#-------

df_w <- data.frame( ID = c(1, 2, 3),
                    TimeStamp = c('12.05.2019 11:34', '12.05.2019 12:34', '12.05.2019 13:34'),
                    A = c(0, 1, 0),
                    B = c(3, 1, 0),
                    C = c(5, 3, 3)
                    )
#http://www.cookbook-r.com/Manipulating_data/Converting_data_between_wide_and_long_format/
cat("WIDE \n")
## WIDE
df_w
##   ID        TimeStamp A B C
## 1  1 12.05.2019 11:34 0 3 5
## 2  2 12.05.2019 12:34 1 1 3
## 3  3 12.05.2019 13:34 0 0 3
cat("\nLONG: reshape \n")
## 
## LONG: reshape
#reshape from (base)
reshape(df_w, 
        direction = "long",
        timevar = "Atribut", 
        v.names = "Vrednost",
        varying = 3:5,
        times = c('A','B','C'))
##     ID        TimeStamp Atribut Vrednost id
## 1.A  1 12.05.2019 11:34       A        0  1
## 2.A  2 12.05.2019 12:34       A        1  2
## 3.A  3 12.05.2019 13:34       A        0  3
## 1.B  1 12.05.2019 11:34       B        3  1
## 2.B  2 12.05.2019 12:34       B        1  2
## 3.B  3 12.05.2019 13:34       B        0  3
## 1.C  1 12.05.2019 11:34       C        5  1
## 2.C  2 12.05.2019 12:34       C        3  2
## 3.C  3 12.05.2019 13:34       C        3  3
cat("\nLONG: tidyr::gather \n")
## 
## LONG: tidyr::gather
library(tidyr)
df_l <- tidyr::gather(df_w, 
              key = "Atribut",
              gathercols = c("A", "B", "C"),
              value = "Vrednost")
df_l
##   ID        TimeStamp Atribut Vrednost
## 1  1 12.05.2019 11:34       A        0
## 2  2 12.05.2019 12:34       A        1
## 3  3 12.05.2019 13:34       A        0
## 4  1 12.05.2019 11:34       B        3
## 5  2 12.05.2019 12:34       B        1
## 6  3 12.05.2019 13:34       B        0
## 7  1 12.05.2019 11:34       C        5
## 8  2 12.05.2019 12:34       C        3
## 9  3 12.05.2019 13:34       C        3
cat("\nv obratni smeri: tidyr::spread \n")
## 
## v obratni smeri: tidyr::spread
tidyr::spread(df_l, key="Atribut", value="Vrednost")
##   ID        TimeStamp A B C
## 1  1 12.05.2019 11:34 0 3 5
## 2  2 12.05.2019 12:34 1 1 3
## 3  3 12.05.2019 13:34 0 0 3
cat("\nLONG: reshape2::melt \n")
## 
## LONG: reshape2::melt
library(reshape2)
reshape2:: melt(df_w, 
                id_vars = c("ID", "TimeStamp"),
                measure.vars = c("A","B","C"),
                variable.name = "Atribut",
                value.name = "Vrednost")
##   ID        TimeStamp Atribut Vrednost
## 1  1 12.05.2019 11:34       A        0
## 2  2 12.05.2019 12:34       A        1
## 3  3 12.05.2019 13:34       A        0
## 4  1 12.05.2019 11:34       B        3
## 5  2 12.05.2019 12:34       B        1
## 6  3 12.05.2019 13:34       B        0
## 7  1 12.05.2019 11:34       C        5
## 8  2 12.05.2019 12:34       C        3
## 9  3 12.05.2019 13:34       C        3
#from excel over clipboard:
#x <- read.table(file = "clipboard", sep = "\t", header=TRUE)

#to excel over cliboard:
#write.table(df_l, "clipboard", sep="\t", dec=",", row.names=FALSE, col.names=TRUE)