Suppose I have a dataframe in R like this
x = c(2, 3.432, 5)
y = c(4.5345, NA, "text")
z = c(8.13451, 3.12451, 6.12341)
A = data.frame(x, y, z)
How do I apply the round function to the appropriate elements of the dataframe? Essentially I want to:
- Check if element is numeric
- If not, make no changes
- If numeric, round it to 3 decimal places
I have read in numerous places that loops are not a good idea in R. Trying lines like
A$y[is.numeric(A$y)] <- round(A$y, digits = 3)
does not work