首页 > 解决方案 > 应用于 R 中数据帧的函数中的嵌套向量

问题描述

我有以下基本数据(问题末尾提供的输入数据):

P <- 72
E_c <- 80000
head (deflections)
    L   I_comp
1  60 17299.21
2  70 25760.94
3  80 32734.69
4  90 51343.59
5 100 60167.30
6 110 64887.87

我想使用以下等式找到最大偏转: 在此处输入图像描述

其中b定义为b = L - aa是一个向量,可以定义为

a <- seq(10,L,10) 

并且x是一个嵌套序列 whitina使得

x <- seq(1,a,1)

我已经编写了下面的函数来计算所有x值的所有a值的最大偏转增量。

delta_less <- function(x){
  a_seq <- seq(10,L,10)
  L <- x[1]
  I_comp <- x[2]
  X_seq <- seq(a_seq)
  delta <- ((P*(L-a_seq)*X_seq)/(6*E_c*I_comp*L))*(L^2-(L-a_seq)^2-X_seq^2)
  c(val = max(delta), pos = which.max(delta))
}

test <- cbind(deflections, vars = t(apply(deflections,1,delta_less)))

问题是我的代码无法识别嵌套序列。只有当我给出a_seq一个奇异的数值时它才有效。

提供的数据:

dput(deflections)
structure(list(L = c(60, 70, 80, 90, 100, 110, 120, 130, 140, 
60, 70, 80, 90, 100, 110, 120, 130, 140, 60, 70, 80, 90, 100, 
110, 120, 130, 140, 60, 70, 80, 90, 100, 110, 120, 130, 140), 
    I_comp = c(17299.2063000114, 25760.9420686703, 32734.6949444858, 
    51343.5889683982, 60167.3001695375, 64887.8729936444, 83451.9473750139, 
    103000.852590734, 112631.744898846, 23539.3443398425, 23544.8558575609, 
    32808.739059987, 40880.4964809276, 60171.1172692296, 64894.6680546358, 
    83469.4777437875, 103114.132264558, 120223.534252571, 23539.3443398425, 
    25772.7904955165, 32810.8231970971, 51345.5936366056, 74673.7079705815, 
    80752.3694583712, 103114.132264558, 103114.132264558, 147386.853127916, 
    23539.3443398425, 25767.8092758621, 40881.3376639665, 55608.9342154021, 
    80694.6568665257, 80752.3694583712, 119068.355471205, 119402.817753462, 
    147386.853127916)), class = "data.frame", row.names = c(NA, 
-36L))

标签: rfunctiondataframeapply

解决方案


推荐阅读