Home > Enterprise >  Rounding Up in an R Data Frame [closed]
Rounding Up in an R Data Frame [closed]

Time:09-23

I have a data frame I created with 7 columns. I want to round up 6 columns by 4 digits and one column by 6 digits. All of the columns are numeric. All in R.

CodePudding user response:

If the first 6 columns needs to be round by 4 and the last by 6

library(dplyr)
df1 <- df1 %>%
    mutate(across(1:6, round, digits = 4), across(7, round, digits = 6))

CodePudding user response:

Using the iris data set the syntax would look like this. You would need to replace the name of your one column that wants to be rounded to six digits from "Sepal.Width" to whatever the name is in your data:

library(dplyr)

iris %>% 
  mutate(across(where(is.numeric), ~ if (cur_column() == "Sepal.Width") round(., 6) else round(., 4)))
  • Related