How to preserve base data frame rownames upon filtering in dplyr chain

前端 未结 4 2000
梦如初夏
梦如初夏 2020-12-05 02:22

I have the following data frame:


df <- structure(list(BoneMarrow = c(30, 0, 0, 31138, 2703), Pulmonar         


        
4条回答
  •  独厮守ぢ
    2020-12-05 02:42

    you can convert rownames to a column and revert back after filtering:

    library(dplyr)
    library(tibble)  # for `rownames_to_column` and `column_to_rownames`
    
    df %>%
        rownames_to_column('gene') %>%
        filter_if(is.numeric, all_vars(. >= 8)) %>%
        column_to_rownames('gene')
    
    #        BoneMarrow Pulmonary
    # ATP1B1         30      3380
    # PRR11        2703        27
    

提交回复
热议问题