0

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.

S31
  • 904
  • 2
  • 9
  • 21
  • 1
    Questions seeking debugging help must include the desired behavior, a specific problem or error and the *shortest code necessary to reproduce it* in the question itself. See [the FAQ](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to learn how to make a great reproducible example. Also, chances are you will identify the bug by yourself in the process of making the minimal reproducible example. – Jealie Apr 17 '17 at 22:22
  • It might be that your function works fine on a single variable but not on a vector. You may need to run it on a vector using `sapply`, or vectorise your function using `Vectorise()`. – Andrew Gustar Apr 17 '17 at 22:47
  • 1
    Why is `x` in quotes in this line `indRel <- severity[severity$industryType == 'x',] ` ? – Andrew Lavers Apr 17 '17 at 23:19
  • Thanks @AndrewGustar - I think you're right, but I haven't used Vectorise() before - would I just Vectorise(lossdfFunc)? Assuming that's not correct as I got an error. – S31 Apr 18 '17 at 03:02
  • @epi99 - in quotes because the parameter x will be a string, x is supposed to stand for the industry type name (Software, Consumer Products etc.) – S31 Apr 18 '17 at 03:03
  • 2
    x should not be in quotes. Quotes indicates it is a literal string. – Andrew Lavers Apr 18 '17 at 03:46
  • It would be useful if you could explain how you are calling the function `lossdfFunc(x)` and what sort of data you are passing it in the parameter `x` – Andrew Gustar Apr 18 '17 at 07:00
  • As well as x not being in quotes. If you want to test it, you would need to call `lossdfFunc("Software")` to pass the literal string to x. – Jeremy Voisey Apr 18 '17 at 13:21
  • @JeremyVoisey epi99 - thank you! It works! Originally I was passing it through as lossdfFunc(Software) with x in quotes, which gave me the error. Changed it to lossdfFunc("Software") and took out the quotes on the x, and it works. Would anyone mind explaining why this works? – S31 Apr 18 '17 at 15:16
  • Anything in quotes is taken literally. `severity$industryType == 'x'` checks to see if industryType is the letter "x". This fails. – Jeremy Voisey Apr 18 '17 at 15:55
  • Without the quotes, it is a variable. `lossdfFunc(Software)` would only work if there was variable called `Software`. – Jeremy Voisey Apr 18 '17 at 15:57
  • `lossdfFunc("Software")` calls your function and assigns "Software" to `x` (a variable). As if you had `x <- "Software"`. At this point `x` is already a string, so you do not need to put quotes around it. – Jeremy Voisey Apr 18 '17 at 16:00
  • @JeremyVoisey ah, got it. Thank you so much! – S31 Apr 18 '17 at 16:57

0 Answers0