How to Round a Data.Frame in R That Contains Some Character Variables

How to round a data.frame in R that contains some character variables?

First make sure your number columns are numeric:

ID = c("a","b","c","d","e")
Value1 = as.numeric(c("3.4","6.4","8.7","1.1","0.1"))
Value2 = as.numeric(c("8.2","1.7","6.4","1.9","10.3"))
df<-data.frame(ID,Value1,Value2, stringsAsFactors = FALSE)

Then, round only the numeric columns:

df[,-1] <-round(df[,-1],0) #the "-1" excludes column 1
df

ID Value1 Value2
1 a 3 8
2 b 6 2
3 c 9 6
4 d 1 2
5 e 0 10

Rounding values in a dataframe in R

In case your data frame contains non-numeric characters you may be willing to make use of the function by Jeromy Anglim:

round_df <- function(x, digits) {
# round all numeric variables
# x: data frame
# digits: number of digits to round
numeric_columns <- sapply(x, mode) == 'numeric'
x[numeric_columns] <- round(x[numeric_columns], digits)
x
}

round_df(data, 3)

I think it's neat and quick approach to handle the rounding problem across heterogeneous data frames.

Rounding numbers in a dataframe with mixed values (string, date, number, etc.)

I guess there is also a tibble solution, but I'm always getting confused on selecting columns etc in tibbles. That's why I changed it into a data.frame.

jnk1 <- as.data.frame(jnk)
numeric_values <- !is.na(apply(jnk1, 2, as.numeric))
jnk1[numeric_values] <- round(as.numeric(jnk1[numeric_values]),1)

Imagine your data is called ink. Save it as a data.frame. Check which values could be parsed into numeric and then overwrite them by the round value.

Since there might be warnings, u can suppress them with this line

numeric_values <- suppressWarnings(!is.na(apply(jnk1, 2, as.numeric)))

Rounding selected columns of data.table

If you don't mind overwriting your original mydf:

cols <- names(mydf)[1:2]
mydf[,(cols) := round(.SD,1), .SDcols=cols]
mydf

# vnum1 vnum2 vch1
#1: 0.6 0.7 B
#2: -1.4 0.5 E
#3: 0.7 0.9 A
#4: -0.3 0.8 C
#5: -0.8 0.6 C

Applying round function to every element in a dataframe

We can try with lapply

A[] <- lapply(A, function(x) if(is.numeric(x)) round(x, 3) else x)

If we need to change the format of numeric elements in those with character/factor class columns as well

A[] <- lapply(A, function(x) {
x1 <- type.convert(as.character(x), as.is=TRUE)
ifelse(grepl("^[0-9.]+$", x1), round(as.numeric(x1), 3), x1)})

How to round a data frame in R which have some non-numeric variables?

This should work if the variables are numeric indeed (you have to rename your columns I suppose as there are some limitations for column names in R):

library(dplyr)

df %>%
dplyr::select(col1 = 1, col2 = 2) %>% # enter all the columns you have and some simple names without special characters
dplyr::mutate_if(is.numeric, ~ round(., 0))

Is there a way to round factors throughout a dataframe using R?

Find the elements that are only numeric and do the rounding in base R itself

i1 <- grep("^[0-9.]+$", df1$Value1)
df1$Value1[i1] <- round(as.numeric(df1$Value1[i1]), 1)

-output

> df1
Group Value1
1 Type A
2 State Florida
3 Roads 107.2

If it is an entire dataset, use lapply

df1[] <- lapply(df1, function(x) {
i1 <- grep("^[0-9.]+$", x)
x[i1] <- round(as.numeric(x[i1]), 1)
x
})

-output

> df1
Group Value1
1 Type A
2 State Florida
3 Roads 107.2


Related Topics



Leave a reply



Submit