Dear all, I am new to R and to it's programming philosophy. The following function is supposed to work on a vector, but I can't figure out how to do that without looping through every element of it. Is there a more elegant way?
Note: I have shortened it, so it is NOT correct from the pipe hydraulics point of view # Calculate wall friction factor # reynolds: Reynolds number # dk: relative roughness of pipe lambda_wall <- function (reynolds, dk) { z <- 1 result <- 0 for (Re in reynolds) { if (Re <= 2320) { # Laminar flow lambda <- 64/Re } else if (Re < 65 * dk[z]) { # Turbulent flow if (Re < 1e+5) { lambda <- 0.3164 / sqrt(sqrt(Re)) } else { lambda <- 0.309/(log10(Re/7))^2 } } else { # Intermediate area lambdanew <- 1 / (2 * log10(3.71 * dk[z]))^2 # Start value iter <- 0 repeat { lambda <- lambdanew lambdanew <- 1 / (2 * log10(2.51/(Re * sqrt(lambda)) + 0.27/dk[z]))^2 iter <- iter + 1 if ((abs(lambdanew - lambda) < 0.001) || (iter > 100)) break } lambda = lambdanew } result[z] <- lambda z <- z + 1 } result } # lambda_wall() ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.