You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Row Name Extraction for data.table() with keep.rownames (#7136)
* Added logic to �s.data.table.list() to preserve names from vectors
* remove trail whit space
* add coverage tests
* replace X and Y
* result test
* tests
* corrected test output
* use as.data.table in tests
* use isFalse
* add classed error conditions
* ws style
* rm redundant condition
* missed ws change
* invalid_input in place of invalid_type
* right place invalid_input
* typo and unsortable in place of unsupported
* specify join type
* merge our loop which checks for vector rowname extraction in below
* added logic for handling data.frame
* add tests
* add tests
* merger master
* remove duplicate
* remove list from as.data.table
* added vignettes
* rm ws
* try to simplify
* fix tests
* restore
* try and handle "inner" row names from matrix case
* rm vestigial
* fix
* simplify tests
* remove any(nzchar(nm))
* remove test condition about any(nzchar(nm))
* update test number
* update description , news.md and add tests
* remove unwanted changes
---------
Co-authored-by: Michael Chirico <michaelchirico4@gmail.com>
Co-authored-by: Michael Chirico <chiricom@google.com>
Copy file name to clipboardExpand all lines: NEWS.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,6 +44,8 @@
44
44
45
45
9.`isoweek()`ismuch faster (e.g.20x) byre-usinganimplementationfrom {base}, [#5111](https://github.com/Rdatatable/data.table/issues/5111). Thanks @MichaelChirico for the report and PR.
46
46
47
+
10.`data.table()`and`as.data.table()`with`keep.rownames=TRUE`nowextractrownamesfromnamedvectors, matching`data.frame()`behavior.Namesfromthefirstnamedvectorintheinputareusedtocreatetherownames column (defaultname`"rn"`orcustomnamevia`keep.rownames="column_name"`), [#1916](https://github.com/Rdatatable/data.table/issues/1916). Thanks to @richierocks for the feature request and @Mukulyadav2004 for the implementation.
48
+
47
49
### BUG FIXES
48
50
49
51
1.Custombinaryoperatorsfromthe`lubridate`packagenowworkwithobjectsofclass`IDate`aswitha`Date`subclass, [#6839](https://github.com/Rdatatable/data.table/issues/6839). Thanks @emallickhossain for the report and @aitap for the fix.
origListNames=if (missing(.named)) names(x) elseNULL# as.data.table called directly, not from inside data.table() which provides .named, #3854
138
138
empty_atomic=FALSE
139
+
140
+
# Handle keep.rownames for vectors (mimicking data.frame behavior)
141
+
rownames_=NULL
142
+
check_rownames=!isFALSE(keep.rownames)
143
+
139
144
for (iin seq_len(n)) {
140
145
xi=x[[i]]
141
146
if (is.null(xi)) next# eachncol already initialized to 0 by integer() above
147
+
if (check_rownames&& is.null(rownames_)) {
148
+
if (is.null(dim(xi))) {
149
+
if (!is.null(nm<- names(xi))) {
150
+
rownames_=nm
151
+
x[[i]] = unname(xi)
152
+
}
153
+
} else {
154
+
if (!is.null(nm<- rownames(xi))) {
155
+
rownames_=nm
156
+
}
157
+
}
158
+
}
142
159
if (!is.null(dim(xi)) &&missing.check.names) check.names=TRUE
143
160
if ("POSIXlt" %chin% class(xi)) {
144
161
warningf("POSIXlt column type detected and converted to POSIXct. We do not recommend use of POSIXlt at all because it uses 40 bytes to store one date.")
Copy file name to clipboardExpand all lines: man/as.data.table.Rd
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ is.data.table(x)
31
31
}
32
32
\arguments{
33
33
\item{x}{AnRobject.}
34
-
\item{keep.rownames}{Defaultis \code{FALSE}.If \code{TRUE}, addstheinputobject's names as a separate column named \code{"rn"}. \code{keep.rownames = "id"} names the column \code{"id"} instead.}
34
+
\item{keep.rownames}{Defaultis \code{FALSE}.If \code{TRUE}, addstheinputobject's names as a separate column named \code{"rn"}. \code{keep.rownames = "id"} names the column \code{"id"} instead. For lists and when calling \code{data.table()}, names from the first named vector are extracted and used as row names, similar to \code{data.frame()} behavior.}
35
35
\item{key}{ Character vector of one or more column names which is passed to \code{\link{setkeyv}}. }
36
36
\item{sorted}{logical used in \emph{array} method, default \code{TRUE} is overridden when \code{key} is provided. }
37
37
\item{value.name}{character scalar used in \emph{array} method, default \code{"value"}.}
0 commit comments