So basically I am doing a physics experiment, and in my table I want to have my data rounded to the same accuracy as the error is, which is rounded to 1 sig fig.
So for example if I have the following:
angle <- c(4, 4.1, 4.2)
angle.error <- c(0.024, 0.3, 0.113)
data <- data.frame(angle,angle.error)
want I want to end up with is the angle error rounded to 1 sig fig, and the angle rounded to the corresponding number of decimal places as the angle error, to give
angle <- c(4.00, 4.1, 4.2)
angle.error <- c(0.02,0.3,0.1)
data <- data.frame(angle, angle.error)
Hope this makes sense!! This is the standard way we are taught to present data, and as such I am surprised how difficult I am finding it to find a proper way of doing this. Any contributions would be great!!
EDIT:
If I want to turn this into a UDR where say the data is in column 5 and the errors in column 6 of my data frame, I write:
Conv2 <- function(x) {
x[6] <- signif(x[6],1)
exp <- floor(log10(x[6]))
man <- x[6]/10^exp
x[5] <- round(x[5], -exp) # Error associated with this line I think
sapply(seq_along(x[5]), function(i) format(x[i,5], nsmall=-exp[i]))
return(x)
}
When I implement it I get the error 'Error in FUN(X[[i]], ...) : non-numeric argument to mathematical function'
angle
rounded to the same number of sigfigs asangle.error
, but your output uses two sigfigs forangle
whileangle.error
only has one. – Vendetta