R:按行列匹配

标签 r match rows

我有两个数据框,如下:
股权数据

ValuationDate   Currency    Opening Closing
02/01/2003        CHF          0    0
02/01/2003        DKK          0    0
03/01/2003        CHF          0    0
02/01/2003        SEK          0    0
03/01/2003        SEK          0    0
04/01/2003        SEK          0    0
05/01/2003        CHF          0    0
03/01/2003        DKK          0    0

其中包含每天以不同货币进行的交易的信息 和历史外汇

Date        CHF        X      DKK     X.1     SEK    X.2
02/01/2003  0.6885  0.688   0.1347  0.1346  0.1094  0.1096
03/01/2003  0.688   0.6858  0.1346  0.1345  0.1096  0.1099
04/01/2003  0.6858  0.6858  0.1345  0.1345  0.1099  0.1099
05/01/2003  0.6858  0.6858  0.1345  0.1345  0.1099  0.1099

其中包含历史外汇汇率,开盘价位于货币代码下方,收盘价位于其旁边的列中。

我正在尝试在 EquityData 数据框中获取相应的外汇价格。

我尝试了以下方法,它有效,但显然效率很低:

 openExchangeMatch = match(EquityData$Currency,colnames(HistoricalFX))
  closeExchangeMatch = match(EquityData$Currency,colnames(HistoricalFX))+1
  dateMatch = match(EquityData$ValuationDate,HistoricalFX$Date)
  for (i in 1:nrow(EquityData))
  {
    EquityData$OpenExchange[i] = HistoricalFX[dateMatch[i],openExchangeMatch[i]]
    EquityData$closeExchange[i] = HistoricalFX[dateMatch[i],closeExchangeMatch[i]]
  }

关于如何以更好的方式解决上述问题有什么想法吗?

最佳答案

我们在对第二个数据集(“df2”,即“HistoricalFX”)进行子集化后创建行/列索引(“indx1”),在第一个数据集(“df1”,即“HistoricalFX”)中分配“Opening”和“Closing”列EquityData') 以及我们在 'op1' 和 'cl1' 中使用 'indx1' 获得的值

op1 <-  df2[-1][c(TRUE, FALSE)]
cl1 <-  df2[-1][c(FALSE, TRUE)]
names(cl1) <- names(op1)
indx1 <- cbind(match(df1$ValuationDate, df2$Date),
              match(df1$Currency, names(op1)))
df1$Opening <- op1[indx1]
df1$Closing <- cl1[indx1]
df1
#  ValuationDate Currency Opening Closing
#1    02/01/2003      CHF  0.6885  0.6880
#2    02/01/2003      DKK  0.1347  0.1346
#3    03/01/2003      CHF  0.6880  0.6858
#4    02/01/2003      SEK  0.1094  0.1096
#5    03/01/2003      SEK  0.1096  0.1099
#6    04/01/2003      SEK  0.1099  0.1099
#7    05/01/2003      CHF  0.6858  0.6858
#8    03/01/2003      DKK  0.1346  0.1345

数据

df1 <- structure(list(ValuationDate = c("02/01/2003", "02/01/2003", 
"03/01/2003", "02/01/2003", "03/01/2003", "04/01/2003", "05/01/2003", 
"03/01/2003"), Currency = c("CHF", "DKK", "CHF", "SEK", "SEK", 
"SEK", "CHF", "DKK"), Opening = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L), Closing = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("ValuationDate", 
"Currency", "Opening", "Closing"), class = "data.frame", row.names = c(NA, 
-8L))

 df2 <- structure(list(Date = c("02/01/2003", "03/01/2003", "04/01/2003", 
"05/01/2003"), CHF = c(0.6885, 0.688, 0.6858, 0.6858), X = c(0.688, 
0.6858, 0.6858, 0.6858), DKK = c(0.1347, 0.1346, 0.1345, 0.1345
), X.1 = c(0.1346, 0.1345, 0.1345, 0.1345), SEK = c(0.1094, 0.1096, 
0.1099, 0.1099), X.2 = c(0.1096, 0.1099, 0.1099, 0.1099)), .Names = c("Date", 
"CHF", "X", "DKK", "X.1", "SEK", "X.2"), class = "data.frame", row.names = c(NA, 
-4L))

关于R:按行列匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30644615/

相关文章:

php - 匹配问题

php - 从 MySQL 表中选择多行

r - 加快 data.table 的向量查找

javascript - 在 R Shiny 的渲染 UI 中创建分类 sliderInput

list - Scala 列表匹配

jquery - 页面加载 jQuery 后匹配 div 高度

r - 使用对象而不是数组将 R 数据帧嵌套到 JSON

r - 如何解决: Could not find package root?错误

根据另一个数据框中的行顺序对一个数据框中的行进行重新排序

python csv按行而不是按列列出