Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue with rownames of pbapply result #44

Closed
KarinSchork opened this issue Aug 19, 2021 · 1 comment
Closed

issue with rownames of pbapply result #44

KarinSchork opened this issue Aug 19, 2021 · 1 comment

Comments

@KarinSchork
Copy link

Hey! First, thanks very much for providing pbapply, it's really helpful!

I noticed a small issue when applying a function that returns a named vector to a data.frame. If the length of the returned vector is the same as the number of columns of the data.frame, then the rownames of the results from pbapply are not taken from the returned vector, but from the colnames of the data.frame. The standard apply function still takes the names from the returned vector.

Here is a minimal example:

library(pbapply)

## function that returns a named vector of length 3:
f <- function(x) {
  return(c(mean = mean(x), median = median(x), sd = sd(x)))
}

### data.frame with 3 columns (= length of returned named vector)
D <- data.frame(matrix(runif(30), ncol = 3))

rownames(apply(D, 1, f))
# [1] "mean"   "median" "sd" 

rownames(pbapply(D, 1, f))
# [1] "X1" "X2" "X3"
### here the rownames are taken form the data.frame instead from the returned vector


### data.frame with 4 columns
D2 <- data.frame(matrix(runif(40), ncol = 4))

rownames(apply(D2, 1, f))
# [1] "mean"   "median" "sd" 

rownames(pbapply(D2, 1, f))
# [1] "mean"   "median" "sd"  
### here the rownames are the same as for apply

@psolymos
Copy link
Owner

Thanks for reporting this. I'll have a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants