I am trying to create a function which lets me go through each row of a data frame and convert the currency this row is in to euro, using the exchange rate of that year using the following code
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]]
}
}
}
}
Using this code to call the function:
Valuta(GBP, 1999 , EURGBPTMP)
R returns:
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
Added Sample Data
Sample for DataFilter:
DataFilter <- data.frame(curcd = c("EUR", "EUR", "GBP", "USD"), fyear
= c("1999", "2000", "2001", "2001"), ebitda = c(63842000, 248290000, 67014000, 34089000), invt = c(107280000, 188001000, 206027000, 185752000), sale = c(414495000, 935212000, 561064000, 518802000))
Sample for EURGBPTMP:
EURGBPTMP <- data.frame(Year = c("1999", "2000", "2001","2002"), Value
= c(0.62170, 0.62410, 0.60850, 0.65050))
With this sample Valuta(GBP, 1999 , EURGBPTMP)
should only edit row 3