melt.array: Melt an array.

Description Usage Arguments See Also Examples

View source: R/melt.r

Description

This code is conceptually similar to as.data.frame.table

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
## S3 method for class 'array'
melt(
  data,
  varnames = names(dimnames(data)),
  ...,
  na.rm = FALSE,
  as.is = FALSE,
  value.name = "value"
)

## S3 method for class 'table'
melt(
  data,
  varnames = names(dimnames(data)),
  ...,
  na.rm = FALSE,
  as.is = FALSE,
  value.name = "value"
)

## S3 method for class 'matrix'
melt(
  data,
  varnames = names(dimnames(data)),
  ...,
  na.rm = FALSE,
  as.is = FALSE,
  value.name = "value"
)

Arguments

data

array to melt

varnames

variable names to use in molten data.frame

...

further arguments passed to or from other methods.

na.rm

Should NA values be removed from the data set? This will convert explicit missings to implicit missings.

as.is

if FALSE, the default, dimnames will be converted using type.convert. If TRUE, they will be left as strings.

value.name

name of variable used to store values

See Also

cast

Other melt methods: melt.data.frame(), melt.default(), melt.list()

Examples

1
2
3
4
5
6
7
8
9
a <- array(c(1:23, NA), c(2,3,4))
melt(a)
melt(a, na.rm = TRUE)
melt(a, varnames=c("X","Y","Z"))
dimnames(a) <- lapply(dim(a), function(x) LETTERS[1:x])
melt(a)
melt(a, varnames=c("X","Y","Z"))
dimnames(a)[1] <- list(NULL)
melt(a)

Example output

   Var1 Var2 Var3 value
1     1    1    1     1
2     2    1    1     2
3     1    2    1     3
4     2    2    1     4
5     1    3    1     5
6     2    3    1     6
7     1    1    2     7
8     2    1    2     8
9     1    2    2     9
10    2    2    2    10
11    1    3    2    11
12    2    3    2    12
13    1    1    3    13
14    2    1    3    14
15    1    2    3    15
16    2    2    3    16
17    1    3    3    17
18    2    3    3    18
19    1    1    4    19
20    2    1    4    20
21    1    2    4    21
22    2    2    4    22
23    1    3    4    23
24    2    3    4    NA
   Var1 Var2 Var3 value
1     1    1    1     1
2     2    1    1     2
3     1    2    1     3
4     2    2    1     4
5     1    3    1     5
6     2    3    1     6
7     1    1    2     7
8     2    1    2     8
9     1    2    2     9
10    2    2    2    10
11    1    3    2    11
12    2    3    2    12
13    1    1    3    13
14    2    1    3    14
15    1    2    3    15
16    2    2    3    16
17    1    3    3    17
18    2    3    3    18
19    1    1    4    19
20    2    1    4    20
21    1    2    4    21
22    2    2    4    22
23    1    3    4    23
   X Y Z value
1  1 1 1     1
2  2 1 1     2
3  1 2 1     3
4  2 2 1     4
5  1 3 1     5
6  2 3 1     6
7  1 1 2     7
8  2 1 2     8
9  1 2 2     9
10 2 2 2    10
11 1 3 2    11
12 2 3 2    12
13 1 1 3    13
14 2 1 3    14
15 1 2 3    15
16 2 2 3    16
17 1 3 3    17
18 2 3 3    18
19 1 1 4    19
20 2 1 4    20
21 1 2 4    21
22 2 2 4    22
23 1 3 4    23
24 2 3 4    NA
   Var1 Var2 Var3 value
1     A    A    A     1
2     B    A    A     2
3     A    B    A     3
4     B    B    A     4
5     A    C    A     5
6     B    C    A     6
7     A    A    B     7
8     B    A    B     8
9     A    B    B     9
10    B    B    B    10
11    A    C    B    11
12    B    C    B    12
13    A    A    C    13
14    B    A    C    14
15    A    B    C    15
16    B    B    C    16
17    A    C    C    17
18    B    C    C    18
19    A    A    D    19
20    B    A    D    20
21    A    B    D    21
22    B    B    D    22
23    A    C    D    23
24    B    C    D    NA
   X Y Z value
1  A A A     1
2  B A A     2
3  A B A     3
4  B B A     4
5  A C A     5
6  B C A     6
7  A A B     7
8  B A B     8
9  A B B     9
10 B B B    10
11 A C B    11
12 B C B    12
13 A A C    13
14 B A C    14
15 A B C    15
16 B B C    16
17 A C C    17
18 B C C    18
19 A A D    19
20 B A D    20
21 A B D    21
22 B B D    22
23 A C D    23
24 B C D    NA
   Var1 Var2 Var3 value
1     1    A    A     1
2     2    A    A     2
3     1    B    A     3
4     2    B    A     4
5     1    C    A     5
6     2    C    A     6
7     1    A    B     7
8     2    A    B     8
9     1    B    B     9
10    2    B    B    10
11    1    C    B    11
12    2    C    B    12
13    1    A    C    13
14    2    A    C    14
15    1    B    C    15
16    2    B    C    16
17    1    C    C    17
18    2    C    C    18
19    1    A    D    19
20    2    A    D    20
21    1    B    D    21
22    2    B    D    22
23    1    C    D    23
24    2    C    D    NA

reshape2 documentation built on April 14, 2020, 5:26 p.m.