Skip to content

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

Closed

Description

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

Activity

  1. juanpide commented on Mar 8, 2017

    @juanpide

    This should be easy to implement and an important addition.

  2. MichaelChirico commented on Mar 8, 2017

    @MichaelChirico
    Member
  3. juanpide commented on Mar 8, 2017

    @juanpide

    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
    
  4. MichaelChirico commented on Mar 21, 2017

    @MichaelChirico
    Member

    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)

  5. added a commit that references this issue on Jul 20, 2019
    2d95232
  6. added this to the 1.12.4 milestone on Aug 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions