FDIC closed 4 more banks and NCUA closed a credit union… things aren’t getting better yet
-
FDIC closed 4 more banks and N…
04-24-2009 by danTags: Twitter | Comments (0)
-
once again, failed to spot Cha…
04-17-2009 by danTags: Twitter | Comments (0)
-
R: tapply Output Formatting
04-13-2009 by danI am often frustrated with the output of tapply. If you aren’t outputting 1 number for each group you are summarizing, the output comes back in a crazy list format that is tough to use.
Here’s an example use of tapply:
# setup data for tapply dt = data.frame(bucket=rep(1:4,25),val=rnorm(100)) fn = function(x) { ret = c(unname(quantile(x,probs=seq(.25,.75,.25),na.rm=T)),mean(x,na.rm=T)) } a = tapply(dt$val,dt$bucket,fn)This code will give you quartiles and a mean for each of the groups (identified by the “bucket” variable).
You can see that the answers are actually there:
> a[1] $`1` [1] -1.1868349 -0.4585898 0.1100985 -0.4729515 > a[2] $`2` [1] -0.50152398 -0.06482399 0.95285490 0.09977271But they are associated with the list items in each location. Unfortunately, this will not work:
b = as.data.frame(a)If you could do this, you would have a nice usable data frame:
b = rbind(a[[1]],a[[2]],a[[3]],a[[4]])But who wants to type out each element of the list everytime you want to construct such a table? Do you always know how many elements are in that list?
The solution is to use the function do.call. This function takes as arguments a function name and some data, and it constructs a call like the one above. Here is the syntax for our example:
b = do.call(rbind,a)This will construct the call to rbind based on the fact that a is a list of items. It is equivalent to calling b = rbind(a[[1]],a[[2]],a[[3]],a[[4]]), but it takes care of all the nuances for you!
For good measure, you may want to bring along the names, too:
row.names(b) = row.names(a) colnames(b) = colnames(a)This will come in very handy for tapply output, but I am sure it will also come in handy for other list-based functions.
-
reading about asthma as an all…
04-08-2009 by danreading about asthma as an allergy to cockroaches: http://tinyurl.com/cyy9f3
Tags: Twitter | Comments (0)
-
iphones and blackberry storms:…
04-07-2009 by danTags: Twitter | Comments (0)
-
if you haven't been watching j…
04-02-2009 by danif you haven’t been watching jim the realtor, you should be: http://www.bubbleinfo.com/
Tags: Twitter | Comments (0)
-
amazing! http://litlurl.net/14…
04-01-2009 by danamazing! http://litlurl.net/141a
Tags: Twitter | Comments (0)