Limiting the number of decimals in a dataframe (R)

后端 未结 4 1846
青春惊慌失措
青春惊慌失措 2021-02-05 08:26

I would like to limit the number of decimals when a data frame is imported. My .txt input have 16 decimals to each row in collumn \"Value\". My dataframe look like that:

4条回答
  •  再見小時候
    2021-02-05 09:20

    Here is.num is TRUE for numeric columns and FALSE otherwise. We then apply round to the numeric columns:

    is.num <- sapply(DF, is.numeric)
    DF[is.num] <- lapply(DF[is.num], round, 8)
    

    If what you meant was not that you need to change the data frame but just that you want to display the data frame to 8 digits then it's just:

    print(DF, digits = 8)
    

    In dplyr 1.0.0 and later one can use across within mutate like this:

    library(dplyr)
    DF %>% mutate(across(is.numeric, ~ round(., 8)))
    

提交回复
热议问题