/
row-col.R
149 lines (128 loc) · 2.47 KB
/
row-col.R
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#' The number of rows/columns
#'
#' `nrow()` and `ncol()` return the number of rows or columns present in x.
#'
#' These functions override base functions to make them generic. The default
#' methods call the base versions.
#'
#' @param x An object.
#' @param ... Other arguments passed on to methods.
#'
#' @return An integer or `NULL`.
#'
#' @name nrow-ncol
NULL
#' @rdname nrow-ncol
#' @export
nrow <- function(x, ...) {
UseMethod("nrow")
}
#' @rdname nrow-ncol
#' @export
nrow.default <- function(x, ...) {
base::nrow(x)
}
#' @rdname nrow-ncol
#' @export
nrow.ddf_col <- function(x, ...) {
nrow_dibble(x)
}
#' @rdname nrow-ncol
#' @export
nrow.tbl_ddf <- function(x, ...) {
nrow_dibble(x, ...)
}
nrow_dibble <- function(x, ...) {
prod(dim(x))
}
#' @rdname nrow-ncol
#' @export
ncol <- function(x, ...) {
UseMethod("ncol")
}
#' @rdname nrow-ncol
#' @export
ncol.default <- function(x, ...) {
base::ncol(x)
}
#' @rdname nrow-ncol
#' @export
ncol.ddf_col <- function(x, ...) {
NULL
}
#' @rdname nrow-ncol
#' @export
ncol.tbl_ddf <- function(x, ...) {
ncol_dibble(x, ...)
}
ncol_dibble <- function(x, ...) {
if (is_ddf_col(x)) {
NULL
} else {
vec_size(colnames(x))
}
}
#' Row and column names
#'
#' Retrieve or set the row or column names of a matrix-like object.
#'
#' These functions override base functions to make them generic. The default
#' methods call the base versions.
#'
#' @param x A matrix-like object.
#' @param ... Other arguments passed on to methods.
#'
#' @return A list of row/column names.
#'
#' @name row-colnames
NULL
#' @rdname row-colnames
#' @export
rownames <- function(x, ...) {
UseMethod("rownames")
}
#' @rdname row-colnames
#' @export
rownames.default <- function(x, ...) {
base::rownames(x, ...)
}
#' @rdname row-colnames
#' @export
rownames.ddf_col <- function(x, ...) {
rownames_dibble(x, ...)
}
#' @rdname row-colnames
#' @export
rownames.tbl_ddf <- function(x, ...) {
rownames_dibble(x, ...)
}
rownames_dibble <- function(x, ...) {
NULL
}
#' @rdname row-colnames
#' @export
colnames <- function(x, ...) {
UseMethod("colnames")
}
#' @rdname row-colnames
#' @export
colnames.default <- function(x, ...) {
base::colnames(x, ...)
}
#' @rdname row-colnames
#' @export
colnames.ddf_col <- function(x, ...) {
colnames_dibble(x, ...)
}
#' @rdname row-colnames
#' @export
colnames.tbl_ddf <- function(x, ...) {
colnames_dibble(x, ...)
}
colnames_dibble <- function(x, ...) {
if (is_ddf_col(x)) {
NULL
} else {
names(x)
}
}