Skip to content

Commit dedec9d

Browse files
committed
Fixes for DataFrame::nrows(), following @romainfrancois commit (closes #120)
1 parent 09928b6 commit dedec9d

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2014-02-23 Kevin Ushey <[email protected]>
2+
3+
* inst/include/Rcpp/DataFrame.h: better nrows behavior
4+
15
2014-02-17 Romain Francois <[email protected]>
26

37
* inst/include/Rcpp/traits/un_pointer.h: fix bug in un_pointer for object<T>

inst/NEWS.Rd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
as the vector .sort() method. These fixes ensure that sugar functions
1616
depending on IndexHash (unique, sort_unique, match) will now properly
1717
handle NA and NaN values for numeric vectors.
18+
\item DataFrame::nrows now more accurately mimics R's internal behavior
19+
(checks the row.names attribute)
1820
}
1921
\item Changes in Rcpp Attributes
2022
\itemize{

inst/include/Rcpp/DataFrame.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ namespace Rcpp{
6060
}
6161

6262
inline int nrows() const {
63-
return Rf_length( VECTOR_ELT(Parent::get__(), 0) );
63+
SEXP rn = Rf_getAttrib( Parent::get__(), R_RowNamesSymbol );
64+
if (TYPEOF(rn) == INTSXP && LENGTH(rn) == 2 && INTEGER(rn)[0] == NA_INTEGER)
65+
return INTEGER(rn)[1];
66+
if (Rf_isNull(rn))
67+
return 0;
68+
return LENGTH(rn);
6469
}
6570

6671
static DataFrame_Impl create(){

0 commit comments

Comments
 (0)