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

feature request : transpose(keep.rownames = T)? #1886

Closed
ghost opened this issue Oct 20, 2016 · 5 comments · Fixed by #3715
Closed

feature request : transpose(keep.rownames = T)? #1886

ghost opened this issue Oct 20, 2016 · 5 comments · Fixed by #3715
Milestone

Comments

@ghost
Copy link

@ghost ghost commented Oct 20, 2016

transposing up to now required this:

dt = data.table(x=1:5, y=6:10)
t(dt) %>% data.table(keep.rownames = T)

, so the transform function has potential to cut this short.
It has however no keep.rownames = T argument (or something similar), so the variable names disappear:

transpose(dt)
#    V1 V2 V3 V4 V5
#1:  1  2  3  4  5
#2:  6  7  8  9 10

what I hoped to see was something like this:

#    rn  V1 V2 V3 V4 V5
#1:  x  1  2  3  4  5
#2:  y  6  7  8  9 10
@skanskan
Copy link

@skanskan skanskan commented Mar 8, 2017

This should be easy to implement and an important addition.

@MichaelChirico
Copy link
Member

@MichaelChirico MichaelChirico commented Mar 8, 2017

@skanskan
Copy link

@skanskan skanskan commented Mar 8, 2017

Imagine you have a data.table with several columns, such as this toy example.
dt = data.table(A=1:5, B=c(NA,3,3,NA,NA), C=c(NA,NA,9,8,7))

and you want to calculate several things to summarize the different variables.

nas <- t(dt[,lapply(.SD,function(x) sum(is.na(x)))])
sums  <- t(dt[,lapply(.SD,function(x) sum(x,na.rm = T))])
data.table(nas=nas,sums=sums)

Something similar to this:
t(apply(dt,2,function(x) rbind(sum(x,na.rm = T),sum(is.na(x)))))

But I having a result with colnames and rownames:

   nas   sums
A   15    0
B    6    3
C   24    2

@ghost
Copy link
Author

@ghost ghost commented Mar 9, 2017

if the example input & output in the original feature request is not clear enough let me know and I will edit.

I agree that a list does not have row.names. The function is also intended for data.frames and data.tables however. When you transpose these objects, their variable names would become row.names, in which the transpose function does not foresee. Hence the need for the verbose as.data.table(t(dt), keep.rownames = T).

@MichaelChirico
Copy link
Member

@MichaelChirico MichaelChirico commented Mar 21, 2017

This could have been useful in this SO post -- instead of needing the helper function coefsplit to break apart the named result of coef(...), we could do transpose(as.list(coef(...)), keep.rownames = TRUE).

This comes at a sort of general thing that I run into a lot of needing to split the named result of a routine into separate columns because the names are important (most salient and consistent example being when I run table by group)

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

Successfully merging a pull request may close this issue.

3 participants