Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I had a very long file of monetary transactions (about 207,000 rows) with about two handfuls of columns describing each transaction (including date). The task I needed to perform on this file was to select the value from one of the categorical descriptor columns (called “appeal”) associated with the first transaction found for every ID in the file. Luckily for me, the file was organized so that the older transactions came before the newer transactions. The coding solution I used was pretty simple, but took maybe 5 – 10 minutes to complete.
Assuming the data frame is called ‘trans.file’, the numerical ID is called, ‘Num.ID’, and the categorical descriptor column of interest is called ‘appeal’, here’s the simple code:
first.appeal.by.id = ddply(trans.file, “Num.ID”, function (x) as.character(x$appeal[1]))
I do like simple, but I also like fast. Is there a less computationally expensive way of doing this? Please tell me if so
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.