Select Page

Functions are great.  You type up a little code, execute your function, and then watch as your automation takes over the world.  Easy enough, right?  Wrong.  Well, sort of wrong.  If you know what you want to do and how to do it, then it’s easy.  If you know what you want to do, but don’t know how to do it, then it’s a little hard.  And if you don’t know what you want to do (and probably wouldn’t now how to do it even if you did), then it’s hard.  Luckily for me, I have a big fat database of Magic the Gathering cards I own and thought it’d be interesting to try to make a function on it.  The easiest thing that i thought I could do would be various analyses on the casting cost of each card.

2_cost

Knowing what I want to do? Check.  The problem for me, though was how my database stored the information.  Normally a card has some numbers and symbols showing how much it costs to play, and that cost can be converted to alphanumerical representation.  For example, the card on the left has a cost of 6RR.  For whatever reason, my database would have displayed that as {6}{R}{R}, which I suppose would be usable if I really knew what I was doing.  But I don’t.  I tried Googling around for how to split columns, but decided that that was over my head.  So I cheated and went into Excel to quickly separate out the values into additional columns, then imported my new data back into R Studio.  I used the count function to

Capture1

Ugly “I don’t know what to do with these” Costs

Capture2

Less Ugly “I can probably manage these” Costs

Make tables of each column and to give a, well, count of each value. From there I figured that I’d need to combine all 7 tables into one, but they had different column headers so I figured that I’d have to find out how to fix that too. I thought that that may take me a while, so I decided to just get started with my function and have it create all of the tables and figure out how to merge them together later.  Well creating the function as essentially a batch command processor turned out to not be as straight forward as anticipated.  I kept having the function try to call the series of

x <- count(Inventory_520782772_2016.February.07truncated, “X”)…

to make all of the tables from calling just my manasymbols function.  Well that didn’t work.  I tried making the tables as a data.frame and as.data.frame.  I tried making the function with manasymbols <- function, manasymbols <- function(), and manasymbols <- function(x).  After a long struggle of trying different combinations of these, I learned 1. that copy/pasting my code into R Studio doesn’t quite make the function properly and 2. that the function won’t make the object a global with just the x <-.  I had to use x <<- in order for the object to be passed from inside of the function to the global level.