File: structure.Rout.save

package info (click to toggle)
r-base 4.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 112,924 kB
  • sloc: ansic: 291,338; fortran: 111,889; javascript: 14,798; yacc: 6,154; sh: 5,689; makefile: 5,239; tcl: 4,562; perl: 963; objc: 791; f90: 758; asm: 258; java: 31; sed: 1
file content (88 lines) | stat: -rw-r--r-- 3,010 bytes parent folder | download | duplicates (3)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

R Under development (unstable) (2022-03-19 r81942) -- "Unsuffered Consequences"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> ### Tests of structure() and deparsing.
> 
> ## deparsing has always treated some attributes specially (apparently
> ## to allow inter-operability with S).
> ## The mapping is
> ## as printed: "dim", "dimnames", "names", "tsp", "levels"
> ## deparsed: ".Dim", ".Dimnames", ".Names", ".Tsp", ".Label"
> ## structure() remaps to the printed form.
> 
> ## The remapping in deparse will be removed in R 4.2.0.
> 
> X <- matrix(1:4, 2, 2, dimnames=list(c("A", "B"), 1:2))
> names(attributes(X))
[1] "dim"      "dimnames"
> cat(deparse(X), "\n")
structure(1:4, dim = c(2L, 2L), dimnames = list(c("A", "B"),      c("1", "2"))) 
> Y <- structure(1:4, .Dim = c(2L, 2L),
+                .Dimnames = list(c("A", "B"), c("1", "2")))
> identical(X, Y)
[1] TRUE
> 
> z <- ts(1:10, frequency = 4, start = c(1959, 2))
> attributes(z)
$tsp
[1] 1959.25 1961.50    4.00

$class
[1] "ts"

> cat(deparse(z), "\n")
structure(1:10, tsp = c(1959.25, 1961.5, 4), class = "ts") 
> z2 <- structure(1:10, .Tsp = c(1959.25, 1961.5, 4), class = "ts")
> identical(z, z2)
[1] TRUE
> 
> ## levels <-> .Label is most relevant to factors, but is always remapped.
> x <- 1:3
> attr(x, "levels") <- letters[x]
> cat(deparse(x), "\n")
structure(1:3, levels = c("a", "b", "c")) 
> y <- structure(1:3, .Label = c("a", "b", "c"))
> identical(x, y)
[1] TRUE
> 
> 
> ## Factors were long deparsed with double (rather than integer codes).
> ## As from R 2.5.0 parsing such a deparse will given an error, so
> ## structure() coerces the codes to an integer vector.
> ## Example from an earlier version of Puromycin.R
> state <- structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+                      2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
+                    .Label = c("treated", "untreated"), class = "factor")
> typeof(state)
[1] "integer"
> storage.mode(state)
[1] "integer"
> attributes(state)
$levels
[1] "treated"   "untreated"

$class
[1] "factor"

> cat(deparse(state))
structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("treated",  "untreated"), class = "factor")> 
> state2 <- structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
+                       2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
+                     levels = c("treated", "untreated"), class = "factor")
> identical(state,state2)
[1] TRUE
>