使用函数创建数据框

时间:2017-06-07 13:40:32

标签: r dataframe

我有一个包含许多值和计算的data.frame。其中一个值是风速。我所做的是通过改变风速来计算不同的能量。我手动完成并逐行构建数据框,如下所示:

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 

这只是我的data.frame的摘录,但我希望你能看到我的意图。 我现在想要的是通过改变参数来创建始终相同的数据帧,例如风速(让我们说3和4米/秒)。 并且同时更改多个值也是很酷的(让我们说风速5和密度1或风速6和密度2)。 我尝试用函数解决这个问题,但它没有用。它只给了我一个值但不是完整的数据框。

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

这是新修订的代码:

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

一个更简单的例子:

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

2 个答案:

答案 0 :(得分:2)

我清理了一下,如果你想要的话,请告诉我:

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

编辑以用于后续问题

您现在遇到的问题完全不同。

您可能想要了解函数debugoncedebug

通过运行:

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

我看到错误在行中:

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

仔细观察,我发现您正在调用V3来定义V3,但V3并不存在,因此该函数会因错误而停止。

我不知道你要做什么,但你不能在右手边使用自己启动变量。

答案 1 :(得分:0)

编辑:更新了两个选项的后续请求。

考虑这种方法;在列中存储参数有点惯用:

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)