1

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")
3
  • The psych package has an incredibly useful read.clipboard() function you can use to copy a data.frame directly from a spreadsheet. Commented Jun 7, 2017 at 13:47
  • 1
    I didn't check all your code, but at the end of the function you should use return(zz). If you don't the function will return the last computed value, in your case zz$V3[5] Commented Jun 7, 2017 at 13:49
  • ok it won't help, there are a lot of messed up stuff there :), I'll try to sort it out Commented Jun 7, 2017 at 13:52

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

you're still storing some numbers as string, and putting row names in a column which is probably not the way you should do it. But I stayed as close to your question as possible.
you may want to check @ddunn801's answer, but then you don't need a data.frame, you could work with a list, and you don't need a function at all
Thanks a lot for your answer, Moody_Mudskipper! After copying your code and adding some more lines and calculations i get the error "object 'V3' not found". I added some calculations which refer to V3 before it was created i think. Do you have an idea how to solve this problem? If it helps i also could edit the question and put in the new code.
you should :), because with this info I can't tell you anything
Do you have any idea?
|
0

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)

5 Comments

Thanks a lot for your answer, ddunn801. Is there any way to add the unit symbols (e.g. m/s, m,...)?
As another row in the dataframe, or actually at the end of each number? Either way is doable.
Then both methods, please :)
Isn't it a good way to store parameters in rows? I did it because then i had a good overview about the unit symbols (first column), given parameters (second column) and calculated parameters (third column).
My 2 cents: I think it's fine to DISPLAY your parameters in columns, but good to STORE them in rows (the way most databases do). Think about the best way to store the potentially zillions of rows and then separately think about how to ask questions of those rows and display results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.