How to short the length of dput

前端 未结 2 1917
死守一世寂寞
死守一世寂寞 2021-01-15 17:31

In the last question I did they pointed out that less data would be easy to read and understand as part of the reproducible example. On the way to asking again I tried to sh

2条回答
  •  耶瑟儿~
    2021-01-15 18:07

    Another way to shorten it up would be to convert the columns to character before dput. The data can then be read back in with as.data.frame and factor levels are preserved.

    First subset

    > data2 <- data[sample(nrow(data), 4), ]
    

    Then dput as characters

    > d <- dput(lapply(data2, as.character))
    structure(list(GOterm = c("GO:0000746", "GO:0070647", "GO:0006914", 
    "GO:0007010"), GOdesc = c("conjugation", NA, NA, "cytoskeleton organization and biogenesis"
    ), GSA_p33_SC = c(NA_character_, NA_character_, NA_character_, 
    NA_character_), GSA_p33_X33 = c(NA, NA, "1", "1"), GSA_p38_SC = c(NA_character_, 
    NA_character_, NA_character_, NA_character_), GSA_p38_X33 = c(NA_character_, 
    NA_character_, NA_character_, NA_character_), GSA_p52_SC = c(NA, 
    "-1", NA, NA), GSA_p52_X33 = c(NA, NA, NA, "1"), GSA_p64_SC = c(NA, 
    NA, NA, "1"), GSA_p64_X33 = c("1", NA, NA, NA), GSA_SC_X33 = c(NA, 
    NA, NA, "1")), .Names = c("GOterm", "GOdesc", "GSA_p33_SC", "GSA_p33_X33", 
    "GSA_p38_SC", "GSA_p38_X33", "GSA_p52_SC", "GSA_p52_X33", "GSA_p64_SC", 
    "GSA_p64_X33", "GSA_SC_X33"))
    

    And read back in

    > as.data.frame(d)
    

提交回复
热议问题