Long: Long und Wide

Description Usage Arguments Value Examples

View source: R/Long.R

Description

Erweiterung von tidyr::pivot_longer tidyr::pivot_wider

Melt2 reshape2::melt()

melt2 ist die lazy_dots-Methode fur reshape2::melt()

Wide entspricht tidyr::spread()

Quelle: https://community.rstudio.com/t/spread-with-multiple-value-columns/5378

Usage

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

## S3 method for class 'formula'
Long(x, data, key = "variable", value = "value", ...)

## S3 method for class 'data.frame'
Long(
  data,
  ...,
  by = NULL,
  key = "variable",
  value = "value",
  id.vars = all.vars(by)
)

## S3 method for class 'list'
Long(x, data, by = NULL, key = NULL, value = NULL, key.levels = NULL, ...)

Melt2(x, ...)

## S3 method for class 'formula'
Melt2(
  x,
  data,
  key = "variable",
  value = "value",
  na.action = na.pass,
  X = stp25formula::prepare_data2(x, data, na.action = na.action),
  id.vars = X$group.vars,
  ...
)

## S3 method for class 'data.frame'
Melt2(x, ..., key = "variable", value = "value")

## Default S3 method:
Melt2(data, ..., key = "variable", value = "value")

melt2(x, ..., by = NULL, key = "variable", value = "value")

Wide(...)

## S3 method for class 'formula'
Wide(x, data, value)

## S3 method for class 'data.frame'
Wide(data, key, value)

Arguments

x

data.frame oder formula

...

weitere Argumente an melt

data

Daten

key

Bezeichnung der Bezeichner-Variable default ist "variable"

value

Bezeichnung der Werte-Variable default ist "value"

by

Gruppierung

id.vars

nur bei Methode data.frame zu verwenden sonst ist hier nichts zu veraendern

key.levels

wenn value gesetzt wird dann 1:nlevels

na.action

auch nicht zu veraendern

X

Formula-Objekt nicht ändern

Value

data.frame

Dataframe in Langfor

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
df <- data.frame(month=rep(1:3,2),
student=rep(c("Amy", "Bob"), each=3),
A=c(9, 7, 6, 8, 6, 9),
B=c(6, 7, 8, 5, 6, 7))

df2<-df %>% Wide(student, c(A, B))



df[-4] %>% tidyr::spread(student, A)
df[-4] %>% Wide(student, A)


df2  %>% Long( Amy_A, Amy_B, Bob_A, Bob_B, by=~month)


df
Long(list( A=c("Amy_A", "Bob_A"), B=c( "Amy_B", "Bob_B")),
     df2,
     by=~month,
     key = "student",
     key.level=c("Amy", "Bob")
)

df %>%
  tidyr::gather(variable, value, -(month:student)) %>%
  tidyr::unite(temp, student, variable) %>%
  tidyr::spread(temp, value)
x<-Melt2(chol0+chol1+chol6+chol12~g , hyper)
aggregate( value~variable, x, mean)

#-- Melt2.data.frame--

x  <- hyper[, c("g","chol0","chol1","chol6","chol12")]
x  <- Melt2(x, id.vars=1)
# aggregate(value~variable+g, x, mean)

# Alternative aber ohne die Labels
x <- hyper  %>%
    tidyr::gather("time", "chol", chol0:chol12) %>%
    dplyr::select(g, time, chol)


 head(x<-Melt2(chol0+chol1+chol6+chol12~g , hyper))
 # APA2(~.,x)

 head( x<- hyper %>%  melt2(chol0,chol1,chol6,chol12, by=~g))
 #APA2(~.,x)

#  suppressPackageStartupMessages(library(tidyverse))

dat <- data.frame(
  month = rep(1:3, 2),
  student = factor(rep(c("Amy", "Bob"), each = 3)),
  A = c(9, 7, 6, 8, 6, 9),
  B = c(6, 7, 8, 5, 6, 7)
)

dat %>% Wide(student,  c(A, B))
dat %>% Wide(student,  c("A", "B"))
dat[-3] %>% Wide(student,  B)
dat  %>% Wide(student ~ month)
#dat[-3] %>% reshape2::dcast(month ~ student)
dat  %>% Wide(month ~ student, A)
dat  %>% Wide(student ~ month, A)

stp4/stp25aggregate documentation built on Sept. 17, 2021, 5:34 a.m.