Baldrian
Baldrian

Reputation: 21

create a data frame with a function

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")

Upvotes: 1

Views: 91

Answers (2)

moodymudskipper
moodymudskipper

Reputation: 47300

I cleaned it a bit, let me know if that's what you want:

speed_vector <- c("w_sp_3" = 3, "w_sp_4" = 4)

make_speed <- function(x)  
{
 V1 <- c("v_in (m/s)","density p (kg/m^3)","Cd ()", "d_rotor (m)", 
             "A_rotor (m^2)")

 V2 <- c(speed_vector[x],  # v_in # it was a string in your question, but it's the only place where x was used so I figured you wanted it as a value
             "1.2",      # density
             "0.00482", # Cd
             "112",     # d_rotor
             "")
  V3 <- c(rep("",4),as.numeric(pi/4*(as.numeric(V2[4]))^2))
  zz <- data.frame(V1,V2,V3) # better to build the data.frame this way than starting with an empty one, and also yours was generating warnings because of number of element not consistent with rows and cols
  return(zz) # the important line that you were missing
}

w_speed_3 <- make_speed("w_sp_3")
w_speed_4 <- make_speed("w_sp_4")

EDIT for your subsequent issue

The issue you're having now is completely different.

You may want to learn about the functions debugonce and debug.

By running :

debugonce(speed)
w_speed_3 <- make_speed("w_sp_3")

I see that the error is in the line :

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))

Looking closer, I see that you're calling V3 to define V3, but V3 doesn't exist, so the function stops with an error.

I don't know what you're trying to do, but you cannot initiate a variable using itself on the right hand side.

Upvotes: 2

ddunn801
ddunn801

Reputation: 1890

EDIT: Updated with the follow-up requests for two options.

Consider this approach; it's a bit more idiomatic to store parameters in columns:

speed_racer <- function(v_in = NULL, density = NULL, Cd = NULL, d_rotor = NULL) {
  scenario <<- data.frame(v_in = c("(m/s)", v_in), density = c("(kg/m^3)", density), 
                          Cd = c("()", Cd), d_rotor = c("(m)", d_rotor), 
                          A_rotor = c("(m^2)", as.numeric(pi/4*(as.numeric(d_rotor)^2))))
}

speed_racer(v_in = 2.0, density = 1.2, Cd = 0.00482, d_rotor = 112.0)

.

speed_racer <- function(v_in = NULL, density = NULL, Cd = NULL, d_rotor = NULL) {
  scenario <<- data.frame(v_in = paste(v_in, "m/s"), density = paste(density, "kg/m^3"), 
                          Cd = Cd, d_rotor = paste(d_rotor, "m"), 
                          A_rotor = paste(as.numeric(pi/4*(as.numeric(d_rotor)^2)), "m^2"))
}

speed_racer(v_in = 2.0, density = 1.2, Cd = 0.00482, d_rotor = 112.0)

Upvotes: 0

Related Questions