i have a data.frame with many values and calculations. One of the values is the wind speed. What i did is to calculate different energies from changing wind speeds. I did it manually and built the data frame row by row just like the following:
zz <- as.data.frame(matrix(seq(100),nrow=5,ncol=3))
zz$V1 <- c("v_in (m/s)","density p (kg/m^3)","Cd ()", "d_rotor (m)",
"A_rotor (m^2)")
zz$V2 <- c("2", # wind_speed
"1.2", # density
"0.00482", # Cd
"112", # d_rotor
"")
zz$V3[1] <- "" # v_in
zz$V3[2] <- "" # density
zz$V3[3] <- "" # Cd
zz$V3[4] <- "" # d_rotor
zz$V3[5] <- as.numeric(pi/4*(as.numeric(zz[4,2]))^2) # A_rotor
This is just an excerpt of my data.frame, but i hope you can see my intention. What i now want is to create always the same data frame with changing parameters, e.g. the wind speed (let's say 3 and 4 m/s). And it would be also cool to change more than one value at the same time (let's say wind speed 5 and density 1 or wind speed 6 and density 2). I tried to solve this problem with a function but it did not work. It only gave me a single value but not the complete data frame.
speed_vector <- c("w_sp_3" = 3, "w_sp_4" = 4)
make_speed <- function(x)
{
zz <- as.data.frame(matrix(seq(100),nrow=5,ncol=3))
zz$V1 <- c("v_in (m/s)","density p (kg/m^3)","Cd ()", "d_rotor (m)",
"A_rotor (m^2)")
zz$V2 <- c("speed_vector[x]", # v_in
"1.2", # density
"0.00482", # Cd
"112", # d_rotor
"")
zz$V3[1] <- "" # v_in
zz$V3[2] <- "" # density
zz$V3[3] <- "" # Cd
zz$V3[4] <- "" # d_rotor
zz$V3[5] <- as.numeric(pi/4*(as.numeric(zz[4,2]))^2) # A_rotor
}
w_speed_3 <- make_speed("w_sp_3")
w_speed_4 <- make_speed("w_sp_4")
This is the new, revised code:
speed_vector <- c("w_sp_3" = 3, "w_sp_4" = 4)
make_speed <- function(x)
{
V1 <- c("v (m/s)","density (kg/m^3)","Cd ()", "d (m)", "A (m^2)", "N/(W*L)",
"length L (km)", "Width W (km)", "Height H (m)", "coefficient ()",
"N ()", "J_h (GW)", "J_v (GW)", "f_red ()", "v (m/s)", "J_out (GW)")
V2 <- c(speed_vector[x], # v
"1.2", # density
"0.00482", # Cd
"112", # d
"",
"4.1", # MW/km^2
"", "",
"700", # Height
"0.44", # coefficient
"", "", "", "", "", "")
V3 <- c(rep("",4),
as.numeric(pi/4*(as.numeric(V2[4]))^2),
rep("",1),
as.numeric((16172.5*0.0898)^(1/2)),
as.numeric((16172.5*0.0898)^(1/2)),
rep("",2),
as.numeric(16172.5*0.0898*as.numeric(V2[4])/3.075),
as.numeric(as.numeric(V2[2])/2*as.numeric(V3[8])*1000*as.numeric(V2[9])*
(as.numeric(V2[1])^3)/10^9),
as.numeric((as.numeric(V2[2])*as.numeric(V2[3])*as.numeric(V3[8])*1000*
as.numeric(V3[7])*1000*(as.numeric(V2[1])^3))/10^9),
as.numeric(as.numeric(V2[9])+2*as.numeric(V2[3])*as.numeric(V3[7])*1000)/
((as.numeric(V2[9])+2*as.numeric(V2[3])*as.numeric(V3[7])*1000)+3/2*
as.numeric(V3[11])/(as.numeric(V3[8])*1000)*as.numeric(V2[10])*
as.numeric(V3[5])),
as.numeric(as.numeric(V3[14])^(1/3)*as.numeric(V2[1])),
as.numeric(as.numeric(V2[2])/2*(as.numeric(V3[8])*1000)*as.numeric(V2[9])*
as.numeric(V3[15])^3/10^9))
zz <- data.frame(V1,V2,V3)
return(zz)
}
w_speed_3 <- make_speed("w_sp_3")
w_speed_4 <- make_speed("w_sp_4")
an easier example:
speed_vector <- c("w_sp_3" = 3, "w_sp_4" = 4)
make_speed <- function(x)
{
V1 <- c("v (m/s)","density (kg/m^3)","Cd ()", "d (m)", "A (m^2)", "N/(W*L)",
"V(m/s)")
V2 <- c(speed_vector[x], # v
"1", # density
"2", # Cd
"3", # d
"4", # MW/km^2
"5", # Height
"6" # coefficient
)
V3 <- c(as.numeric(as.numeric(V2[4]) ^ 2),
as.numeric(as.numeric(V2[1]) + as.numeric(V2[2])),
as.numeric(as.numeric(V2[1]) * as.numeric(V2[2])),
as.numeric(as.numeric(V2[1]) + as.numeric(V2[2]) + as.numeric(V2[3])),
as.numeric(as.numeric(V2[2]) + as.numeric(V3[2])),
as.numeric(as.numeric(V3[1]) + as.numeric(V3[2])))
zz <- data.frame(V1,V2,V3)
return(zz)
}
w_speed_3 <- make_speed("w_sp_3")
w_speed_4 <- make_speed("w_sp_4")
psychpackage has an incredibly usefulread.clipboard()function you can use to copy adata.framedirectly from a spreadsheet.return(zz). If you don't the function will return the last computed value, in your casezz$V3[5]