Creating a function to feed in an industry type to pull it's corresponding frequency, and I keep getting the same error:
Error in data.frame(mainDF, avg, med) :
arguments imply differing number of rows: 112, 0
This is my function -
lossdfFunc <- function(x) {
indRel <- severity[severity$industryType == 'x',]
weightPercent <- indRel$relsev
avg <- (mainDF$Amount/mainDF$Count)*weightPercent
avg <- dec_amt(avg, 2)
med <- mainDF$medAmount*weightPercent
lossesDF <- data.frame(mainDF, avg, med)
return(lossesDF)
}
If I run this function line by line, it works perfectly fine and it will just put NA for the last few rows of the dataframe that don't have a medAmount value for the med column (which is what I'd like).
The mainDF essentially looks like:
damageType Year Count Amount medAmount
RR 2000 4 10000 5000
RR 2001 2 50210 125019
CD 2000 5 55000 159801
CD 2001 1 25000 12901
PI 2000 15 100 152098
PI 2001 20 15200 77655
TD 2000 7 50000 NA
TD 2001 11 12511 NA
severityDF looks like:
industryType relfreq relsev
1 Consumer Products 2.032520 0.419048
2 Biotech/Pharma 0.650407 3.771429
3 Industrial/Construction 1.327913 0.609524
4 Computer Hardware/Electronics 1.571816 2.019048
5 Medical Devices 1.463415 3.028571
6 Software 0.758808 1.314286
My ideal output (and the output I get if I feed in line by line) is (this is assuming Software is plugged in for x in the lossdfFunc):
Amount medAmount avg med
1 10000 5000 13142.8600 6571.43
2 50210 125019 65990.3001 164310.72
3 55000 159801 72285.7300 210024.22
4 25000 12901 32857.1500 16955.60
5 100 152098 131.4286 199900.27
6 15200 77655 19977.1472 102060.88
7 50000 NA 65714.3000 NA
8 12511 NA 16443.0321 NA
I can't help but think the issue might be with that first line
indRel <- severity[severity$industryType == 'x',]
because when I type in Software into x and rerun the function, it works perfectly fine by giving me the Software dataframe. But if I leave it as x, type in lossdfFunc(Software), I get the above error about differing rows.