【问题标题】:Calculation in R involving two subscriptsR中涉及两个下标的计算
【发布时间】:2020-09-02 17:07:23
【问题描述】:
data1 <- structure(list(A = c(2755964268, 2755964268, 375194875,  
263031965, 
8499219585, 499373653.92, 280423947, 2158547197, 1128997565.89, 
1128997567, 1670585623, 3790282427.29, 3790282427.84, 73173066, 
195366117, 1642701650, 349564838), s2aum = c(0.241, 0.241, 
0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 
0.241, 0.241, 0.241, 0.241, 0.241, 0.241), s2mkt = c(0.31, 
0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 
0.31, 0.31, 0.31, 0.31, 0.31), b = c(0.625, 1.162, 
0.953, 0.002, 0.61, 0.948, 0.654, 0.002, 0.656, 0.552, 0.923, 
0.653, 0.002, 0.051, 0.032, 0.033, 0.05)), row.names = c(NA, 
-17L), class = c("tbl_df", "tbl", "data.frame"))

data2<-structure(list(A = c(400541354, 1759793458, 4596389511, 
400541354, 
64654237, 478899312, 5417062, 18592832, 18667854, 61344762, 25868625, 
5365055584, 5365055584, 27595349, 2457211376, 229229952, 6362880477, 
1250770399, 1570627004, 71104043, 71104043, 52225686, 1613077749, 
232150795, 5239674408, 61344762, 18667854, 27595349, 2832937760, 
3614230, 3614230, 475663543, 1289565081, 182105777, 182105777, 
182105777, 51124786, 51124786, 48653333, 2781054257, 51343213, 
234285653, 5365055584, 6711129454, 1532814176, 883507188, 883507188, 
691061024, 1613077749, 1613077749, 1613077749, 20095892222, 232150795, 
283099276, 283099276, 17720976, 5365055584, 5365055584, 1350545862, 
1025530733, 2781054257, 946525804, 995411261, 45532725, 232150795
), s2aum = c(0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 
0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 
0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 
0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 
0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 
0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 
0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 0.241, 
0.241, 0.241, 0.241, 0.241), s2mkt = c(0.31, 0.31, 0.31, 
0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 
0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 
0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 
0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 
0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 
0.31, 0.31, 0.31, 0.31, 0.31, 0.31, 0.31), b = c(0.895, 
1.102, 1.23, 0.959, 0.242, 1.011, 0.002, 1.311, 1.012, 0.988, 
1.146, 0.981, 0.336, 1.252, 0.858, 0.944, 1.034, 1.144, 0.931, 
0.273, 0.866, 0.947, 0.223, 0.934, 0.974, 0.331, 0.309, 0.897, 
0.958, 0.638, 1.107, 0.858, 0.999, 0.929, 0.957, 0.928, 0.625, 
1.057, 0.945, 0.931, 0.958, 1.011, 0.98, 0.867, 1.205, 0.929, 
0.236, 0.889, 0.945, 0.946, 0.804, 0.935, 0.933, 0.059, 0.959, 
0.849, 0.981, 0.98, 0.841, 1.279, 1.161, 0.985, 1.23, 1.26, 0.957
)), row.names = c(NA, -65L), class = c("tbl_df", "tbl", "data.frame"
))

我需要为每i行data1计算,有j行data2

我想我需要首先为所有js 修复i=1,然后为所有js 修复i=2,然后为所有j 修复i=3等。

我需要计算的公式是: [data1 和 data2 的 s2aum 和 s2mkt 相同]

数字_(i,j)=(A_i-A_j )^2/s2aum+(b_i-b_j )^2/s2mkt

我尝试了以下代码:

Number <- matrix()
for (i in 1:nrow(data1)){
  for (j in 1:nrow(data2)){
    Number[i,j] = (data1$A[i] - data2$A[j])^2 / data1$s2aum + 
      (data1$b[i] - data2$b[j])^2 / data2$s2mkt
  print(Number[i,j])
    }
}

但它有错误。

【问题讨论】:

    标签: r


    【解决方案1】:

    不确定您所说的“我需要为每 i 行数据 1 计算,有 j 行数据 2”是什么意思,因为公式和代码尝试使它看起来像您想要为 1 的每个组合获取一个值来自 data1 的行和来自 data2 的 1 行。

    看看这对你是否有意义:

    library(dplyr)
    
    # create a data frame where each row is a i_j combination of rows from data1 & data2
    result <- expand.grid(row.1 = seq(1, nrow(data1)), 
                          row.2 = seq(1, nrow(data2)),
                          KEEP.OUT.ATTRS = FALSE) %>%
      
      # add each dataset to the combined data frame
      left_join(data1 %>% mutate(row.1 = seq(1, n())) %>%
                  rename(A1 = A, b1 = b),
                by = "row.1") %>%
      left_join(data2 %>% mutate(row.2 = seq(1, n())) %>% 
                  rename(A2 = A, b2 = b) %>%
                  select(A2, b2, row.2), by = "row.2") %>%
      
      # calculate result for each i_j combination
      mutate(value = (A1 - A2)^2 / s2aum + (b1-b2)^2 / s2mkt) %>%
      
      # sort results in order
      arrange(row.1, row.2)
    
    # put results in appropriate matrix form
    result.matrix <- matrix(result$value, nrow = nrow(data1), byrow = TRUE)
    

    【讨论】:

    • 谢谢你,这太棒了!我现在正在研究它。谢谢!
    • 非常感谢!这对我来说很有意义。谢谢!
    • 很高兴为您提供帮助。 :) 还有一点建议:冒着过度概括的风险,循环几乎永远不是在 R 中执行某些任务的答案。R inferno 中的 Circle 3 对此进行了很好的讨论。
    • 请问,哪本书最适合开发R编程,比如写递归算法,lapply,mapply,相当于循环的东西等等?我想我是中级 R 用户,但我没有计算机科学或数学学位,谢谢! @Z.Lin
    • 不确定是否有关于 R 的单一“最佳”指南,但以下可能是开始浏览的好地方:bigbookofr.com/index.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多