从R函数中调用不同的数据帧

我正在尝试创建一个函数,该函数可让我浏览数据框的每一行并将该行的货币转换为欧元,使用当年的汇率,并使用以下代码

Valuta <- function(Code, Year, Exchange) {
  for (i in 1:nrow(DataFilter)) {
    if (DataFilter[i,'curcd'] == deparse(substitute(Code))) {
      if (DataFilter[i,'fyear']== deparse(substitute(Year))) {
          DataFilter[i,'ebitda'] <- DataFilter[i,'ebitda'] / Exchange[,'Value'][Exchange[,'Year'==Year]]
          DataFilter[i,'invt'] <- DataFilter[i,'invt'] / Exchange[,'Value'][Exchange[,'Year'==Year]]
          DataFilter[i,'sale'] <- DataFilter[i,'sale'] / Exchange[,'Value'][Exchange[,'Year'==Year]]
      }
    }
  }
}

使用以下代码调用该函数:

Valuta(GBP, 1999 , EURGBPTMP)

R返回:

Error in Exchange[, "Value"][Exchange[, "Year" == Year]] : 
  invalid subscript type 'list'

GBP is the call sign in the data frame DataFilter

EURGBPTMP is a data frame containing 2 columns: Year and Value, where each Year is unique

Using unlist() returns the same result, even though the result of Exchange[,'Value'][Exchange[,'Year'==Year]] should not be a list

评论