[R] Why do data frame column types vary across apply, lapply?

2010-04-30 Thread Jeff Brown
Hi, I still have little ability to predict how these functions will treat the columns of data frames: # Here's a data frame with a column a of integers, # and a column b of characters: df - data.frame( + a = 1:2, + b = c(a,b) + ) df a b 1 1 a 2 2 b # Except -- both columns

Re: [R] How to work out 3-way probabilities

2010-04-27 Thread Jeff Brown
If you're willing to assume independence, multiplication is the way to go: Pr( a beats b a beats c) = Pr( a beats b ) * Pr( a beats c ) If you're not willing to assume independence, things can get very weird: http://en.wikipedia.org/wiki/Arrow's_impossibility_theorem -- View this message in

Re: [R] Count matches of a sequence in a vector?

2010-04-21 Thread Jeff Brown
One option might be to turn the sequence into a character string, and then use something like grep(). Kind of a kludge, but possibly easy. -- View this message in context: http://n4.nabble.com/Count-matches-of-a-sequence-in-a-vector-tp2019018p2019161.html Sent from the R help mailing list

Re: [R] Count matches of a sequence in a vector?

2010-04-21 Thread Jeff Brown
Phil's algorithm is a good one, unless you're worried about optimizing for speed. It makes N * M comparisons, where N is the length of the first vector and M is the length of the second. Explicitly iterating through the longer vector, you could reduce the number of comparisons to M. As is

Re: [R] Count matches of a sequence in a vector?

2010-04-21 Thread Jeff Brown
This sort of calculation can't be vectorized; you'll have to iterate through the sequence, e.g. with a for loop. I don't know if a routine has already been written. -- View this message in context: http://n4.nabble.com/Count-matches-of-a-sequence-in-a-vector-tp2019018p2019108.html Sent from

Re: [R] xtabs() of proportions, and naming a dimension (not a row)

2010-04-19 Thread Jeff Brown
Thanks a lot, David and Dennis! Also, your suggestions for how I could have better stated my question are duly noted, and appreciated. -- View this message in context: http://n4.nabble.com/xtabs-of-proportions-and-naming-a-dimension-not-a-row-tp2015261p2015380.html Sent from the R help mailing

Re: [R] dataframe

2010-04-19 Thread Jeff Brown
http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-do-I-convert-factors-to-numeric_003f -- View this message in context: http://n4.nabble.com/dataframe-tp2015650p2016230.html Sent from the R help mailing list archive at Nabble.com. __

[R] xtabs() of proportions, and naming a dimension (not a row)

2010-04-18 Thread Jeff Brown
Hi, xtabs() creates a table of counts. I want a table of proportions -- that is, I want to divide every vector (along a particular dimension) by its sum. The tiny example below does that. The call to xtabs() creates a matrix A with dimensions (x1,x2,y). I transform A using aperm() and

Re: [R] str: how to use no list recursively?

2010-04-11 Thread Jeff Brown
Wow, those are cool functions!  Thanks! --- On Sat, 4/10/10, Henrique Dallazuanna [via R] ml-node+1835454-939980203-179...@n4.nabble.com wrote: From: Henrique Dallazuanna [via R] ml-node+1835454-939980203-179...@n4.nabble.com Subject: Re: str: how to use no list recursively? To: Jeff Brown

[R] str: how to use no list recursively?

2010-04-09 Thread Jeff Brown
Hi, In the help file for str(), the following line appears: no.list logical; if true, no ‘list of ...’ nor the class are printed. However, that appears to be true only on the top level; setting no.list to TRUE still leaves the remaining levels with the `list of ...' statement intact:

Re: [R] Scope and assignment: baffling

2010-04-06 Thread Jeff Brown
Thanks, Bill! That was deep, and took me a long time to work through, but I get it now. And Gabor -- r-proto is great! -- View this message in context: http://n4.nabble.com/Scope-and-assignment-baffling-tp1747582p1753321.html Sent from the R help mailing list archive at Nabble.com.

[R] Export bug? Hist() vs. barchart().

2010-04-03 Thread Jeff Brown
Hi, If you want to export a single bar chart, this works: png( ET ); barchart( data[,ET] ) dev.off() quartz_off_screen 2 But if you want to export a few of them, this does not: factorCols - c(MR,ET); sapply( factorCols, function(x) { + png( x ); + barchart(

Re: [R] Export bug? Hist() vs. barchart().

2010-04-03 Thread Jeff Brown
You were right. Jorge Velez wrote me off-list and pointed out that I need to call print() to write the barplot to the device, just as explained in the FAQ. Thanks! -- View this message in context: http://n4.nabble.com/Export-bug-Hist-vs-barchart-tp1750593p1750672.html Sent from the R help

Re: [R] Sharing levels across multiple factor vectors

2010-04-02 Thread Jeff Brown
Ah, I finally figured it out: I had asked In both of those cases, why is the [] needed? It's because when on the left hand side of an assignment, the bracket operator attempts to preserve the class and dimension of the object it's subsetting. (Or at least, that's true when the object is a

[R] Scope and assignment: baffling

2010-04-01 Thread Jeff Brown
Hi, The code below creates a value, x$a, which depending on how you access it evaluates to its initial value, or to what it's been changed to. The last two lines should, I would have thought, evaluate to the same value, but they don't. f - function () { x - NULL; x$a - 0;

Re: [R] Sharing levels across multiple factor vectors

2010-04-01 Thread Jeff Brown
Wow, those are much more elegant. Thanks! Peter suggests: df[] - lapply(df, factor, levels=allLevels, labels=seq_along(allLevels)) Henrique suggests: df[] - as.numeric(unlist(df)) In both of those cases, why is the [] needed? When I evaluate df vs. df[], they both look the same, but

[R] Sharing levels across multiple factor vectors

2010-03-31 Thread Jeff Brown
Hi, I've got a data frame with multiple factor columns, but they should share the same set of labels, such as this tiny example: df - data.frame ( a = factor( c( bob, alice, bob ) ), b = factor( c( kenny, alice, alice ) ) ); In my data, though, the strings are enormous. I

Re: [R] Sharing levels across multiple factor vectors

2010-03-31 Thread Jeff Brown
Sorry for spamming. I swear I had worked on that problem a long time before posting. But I just figured it out: I have to change the values, which are represented as integers, not strings. So the following code will do it: df - data.frame ( a = factor( c( bob, alice, bob ) ),

Re: [R] tapply syntax

2010-03-28 Thread Jeff Brown
What is the function set()? Is that a typo? When I type ?set I get nothing, and when I try to evaluate that code R tells me it can't find the function. -- View this message in context: http://n4.nabble.com/tapply-syntax-tp1692503p1694586.html Sent from the R help mailing list archive at

Re: [R] Heirarchical tree data structure

2010-03-28 Thread Jeff Brown
When I tried ??tree, I found something called dendrogram that looks like it might be what you want. -- View this message in context: http://n4.nabble.com/Heirarchical-tree-data-structure-tp1692838p1694575.html Sent from the R help mailing list archive at Nabble.com.

[R] c(), or cat(), or paste(), all cause unwanted reordering

2010-03-25 Thread Jeff Brown
Hi, I would expect the following: paste( as.character( cat( rep( ., 2 ) ) ), a string, as.character( cat( rep( ., 3 ) ) ) ); to yield this string: . . a string . . ., but instead it yields this: . .. . .[1] a string The third argument has been stuck immediately

Re: [R] c(), or cat(), or paste(), all cause unwanted reordering

2010-03-25 Thread Jeff Brown
Wow, you guys are awesome. Thanks! -- View this message in context: http://n4.nabble.com/c-or-cat-or-paste-all-cause-unwanted-reordering-tp1691133p1691198.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing

Re: [R] Interleaving elements of two vectors?

2010-03-25 Thread Jeff Brown
I just had to solve this problem for myself, after not having luck with the code posted above. I'm posting in case others need a completely general function. riffle - function (a,b) { # Interleave a b, starting with a, without repeating. x - NULL; count = 1;