Skip to content

[Rcpp-devel] Getting names

3 messages · David Edwards, Søren Højsgaard

#
Another novice question: the function

// [[Rcpp::export]]
IntegerVector maketable(IntegerVector x) {
  IntegerVector tab=table(x);
  return tab;
}

returns a named vector, and is about twice as fast as the R function 'table'. Is there a way to extract the names of the vector as a CharacterVector (or even better here, as an IntegerVector)?

BR David


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140325/efe7ab07/attachment-0001.html>
#
Something like this?

x <- c(a=1, b=2)
f <- cppFunction("CharacterVector foo(NumericVector x){return x.names();}")
[1] "a" "b"

S?ren


From: rcpp-devel-bounces at lists.r-forge.r-project.org [mailto:rcpp-devel-bounces at lists.r-forge.r-project.org] On Behalf Of David Edwards
Sent: 25. marts 2014 10:45
To: 'rcpp-devel at lists.r-forge.r-project.org'
Subject: [Rcpp-devel] Getting names

Another novice question: the function

// [[Rcpp::export]]
IntegerVector maketable(IntegerVector x) {
  IntegerVector tab=table(x);
  return tab;
}

returns a named vector, and is about twice as fast as the R function 'table'. Is there a way to extract the names of the vector as a CharacterVector (or even better here, as an IntegerVector)?

BR David


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140325/88df4ce0/attachment.html>
#
Super, thanks for this
Regards
David

From: S?ren H?jsgaard [mailto:sorenh at math.aau.dk]
Sent: 25. marts 2014 11:03
To: David Edwards; 'rcpp-devel at lists.r-forge.r-project.org'
Subject: RE: Getting names

Something like this?

x <- c(a=1, b=2)
f <- cppFunction("CharacterVector foo(NumericVector x){return x.names();}")
[1] "a" "b"

S?ren


From: rcpp-devel-bounces at lists.r-forge.r-project.org<mailto:rcpp-devel-bounces at lists.r-forge.r-project.org> [mailto:rcpp-devel-bounces at lists.r-forge.r-project.org] On Behalf Of David Edwards
Sent: 25. marts 2014 10:45
To: 'rcpp-devel at lists.r-forge.r-project.org'
Subject: [Rcpp-devel] Getting names

Another novice question: the function

// [[Rcpp::export]]
IntegerVector maketable(IntegerVector x) {
  IntegerVector tab=table(x);
  return tab;
}

returns a named vector, and is about twice as fast as the R function 'table'. Is there a way to extract the names of the vector as a CharacterVector (or even better here, as an IntegerVector)?

BR David


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140325/d741f7dd/attachment.html>