0

I am doing a very simple operation where I create a new column in dataframe with values as 3.1+0.2. I want this new column as numeric and so I cast the operation as as.numeric. For some reason, the new column values are still characters, as can be seen below:

df <- data.frame(col=c(2, 3, 5))
df$newCol <- as.numeric(3.1+0.2)
filter(df, newCol == 3.3)

The filter for newCol == 3.3 results in zero rows.

[1] col    newCol
<0 rows> (or 0-length row.names)

However, if I filter newCol value as characters then it shows all records.

> filter(df, newCol == '3.3')
  col newCol
  1   2    3.3
  2   3    3.3
  3   5    3.3

It is weird that a simple operation converts the values into characters by default, and doesn't convert it into numeric even after using as.numeric.

Gaurav
  • 193
  • 1
  • 1
  • 12

0 Answers0