I have a dataset with the length and width of an prolate spheroid:
df <- data.frame(nr = c(1, 2, 3), length = c(4, 5, 3), width = c(2, 2, 1))
df
Now I want to make an extra column with the volume. I've used the formula V = 4/3*pi*a²b (with a and b = 1/2 length and width respectively):
df$volume <- (4/3)*pi*(df$length/2)^2*(df$width/2)
This works, but I want to know if there is maybe an inbuilt formula for this in R?
I'm not aware of such a function, and I'd be a bit surprised to see it implemented somewhere, just because it's a somewhat esoteric geometry thing. (Every esoteric stats concept has been implemented somewhere in R or in a package, but esoteric geometry concepts are much rarer since R is not a tool designed with geometric applications specifically in mind.)
That said: whether or not such a thing has already been implemented somewhere, why not just write a custom function using the code you gave?
spheroid_vol <- function(length, width){
4/3 * pi * (length/2)^2 * (width/2)^2
}
> spheroid_vol(df$length, df$width)
# 16.755161 26.179939 2.356194
You could also make this a better function by doing things like checking to make sure the inputs are nonempty, etc. -- but that may or may not be worth the effort depending on what you want to do with it.