Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a dataframe column contains decimal values

Tags:

r

Sample Dataframe

df<- data.frame(column1=c("row1", "row2", "row3", "row4", "row5", "row6"), column2=c(1,3,4.3,5,6.5,7.8))

check if df$column2 contains decimals. The result should be a TRUE or FALSE

like image 829
Fred Kwebiha Avatar asked Oct 15 '25 08:10

Fred Kwebiha


2 Answers

Try this:

round(df$column2) != df$column2

Since you cannot create a column of internals and doubles, we improperly assume that if the decimal part is 0, then the number is integer.

If you are interested in knowing if the column contains at least one decimal use

any(round(df$column2) != df$column2)

If you are interested in knowing if all the column values ​​are decimal use

all(round(df$column2) != df$column2)
like image 143
Leonardo Avatar answered Oct 17 '25 00:10

Leonardo


Use the modulus operator.

x%%1==0 will check if the modulus (x mod 1) is 0

x%%1!=0 will check if the modulus (x mod 1) is not 0, which means its value has decimals

This will give you elementwise results:

> df$column2%%1!=0
[1] FALSE FALSE  TRUE FALSE  TRUE  TRUE

for an aggregate on the whole column, call

any(df$column2%%1!=0)

[1] TRUE
like image 40
GuedesBF Avatar answered Oct 17 '25 01:10

GuedesBF



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!