Wednesday, October 10, 2012

Looping through variable names in R

Ok, so most people probably know how to do this, but I have a mental block every time I have to do this.  If you have a list of variable names for which you want to do something, here are several ways to do it.

> set.seed(1234567)
> dat=data.frame(matrix(rbinom(100, 5,.3), ncol=5))
> head(dat)
  X1 X2 X3 X4 X5
1  2  3  1  2  1
2  2  1  1  3  0
3  3  3  2  1  2
4  0  1  2  2  2
5  2  1  0  1  1
6  1  1  3  2  2
> nms=names(dat)
> for(i in 1:length(nms)){
+   print(with(dat, eval(parse(text=paste("table(",nms[i],")")))))
+ }
X1
0 1 2 3 
3 7 6 4 
X2
0 1 2 3 4 5 
2 9 4 3 1 1 
X3
0 1 2 3 
4 8 6 2 
X4
 0  1  2  3 
 1 11  6  2 
X5
0 1 2 3 4 
3 7 8 1 1  
The innermost paste() component produces a character string which is the function to be evaluated.
 
> i=5 
> paste("table(",nms[i],")")
[1] "table( X5 )"
It is then parsed as text, and evaluated within the dat environment.  Finally, because the output can be displayed by wrapping print() around the whole expression.  This approach is proposed here

Alternatively, one could use lapply() and avoid the loop as proposed by UCLA ATS here.

> with(dat, lapply(names(dat), 
+                  function(x){
+                    table(eval(substitute(tmp, list(tmp=as.name(x)))))
+                  }))
[[1]]

0 1 2 3 
3 7 6 4 

[[2]]

0 1 2 3 4 5 
2 9 4 3 1 1 

[[3]]

0 1 2 3 
4 8 6 2 

[[4]]

 0  1  2  3 
 1 11  6  2 

[[5]]

0 1 2 3 4 
3 7 8 1 1

Note that this method doesn't print a name for each table. This problem can be solved by using sapply(), the "user-friendly version and wrapper of lapply()", and specifying the USE.NAMES=TRUE option.

> with(dat, sapply(names(dat), 
+                  function(x){
+                    table(eval(substitute(tmp, list(tmp=as.name(x)))))
+                    }, 
+                  USE.NAMES=TRUE))
$X1

0 1 2 3 
3 7 6 4 

$X2

0 1 2 3 4 5 
2 9 4 3 1 1 

$X3

0 1 2 3 
4 8 6 2 

$X4

 0  1  2  3 
 1 11  6  2 

$X5

0 1 2 3 4 
3 7 8 1 1 


In Stata there is a designated command for this - one could simply use
foreach var of varlist X1-X5{
   tab `var'
}

Thursday, September 27, 2012

Word 2010: Linear format equations tips and tricks

Word 2010 has a nifty, almost LaTeX-like equation typesetting interface (as opposed to Equation Editor in previous versions).  To insert an equation using the keyboard, press ALT+=, and then type the equation. The most up-to-date documentation is available online (it's also viewable in Word help files under Equations in Windows, but does not appear on Mac Office 2011).

Word uses "Unicode Nearly Plain-Text Encoding for Mathematics" for mathematical expressions. It follows "Unicode Technical Note 28" convention. Whereas one can wing it without learning Unicode syntax and by mostly sticking to LaTeX, there is definitely more to this.

Today we resolved two more complex issues. One was how to typeset multi-line equations, and the second one was how to add equation numbering.

Q1:  How to typeset multi-line equaions? 
A1:  Turns out, it's quite simple within the \eqarray environment. Similar to LaTeX.  Use & to indicate where to align, and @ to indicated where to line break. It's now well documented, see the very first example. An explanation of how this works and more complex examples can be found in Murray Sargent's blog post.

Q2:  How to number equations?
A2:  As Technical Note 28 suggests, inserting #(1), or \eqno(1),  after the equation should add a right-justified equation number (Section 3.21).  However, it doesn't work. At least not in Word.  It does, however, work in PowerPoint (and supposedly in Excel and OneNote).  See here for an explanation of how numbering should work.  See comment from user Nali (26nov2009) about how to do it in other Office products. On a related note, \eqno is not listed in the "Math AutoCorrect symbols" in Word equaion documentation, so it's clearly not supported.

Q3:  Ok then, so what if I'm using Word?
A3:  To get equation numbering for equations in display mode:
  1. Insert a 3 x 1 table with gridlines but no borders. Set table to take 100% of page width, and columns to take roughly 10%, 80% and 10%, respectively. Set vertical alignment of columns to center, so that for a multi-line equation, the equation number remains centered.
  2. To insert the equation number, simply add a numbered item to the rightmost (or leftmost) cell. Since "(#)" is not a built-in numbering format, you may need to create it first. This will provide automatic numbering of subsequent equations. Alternatively, you can simply specify the equation numbers manually.
  3. The limitation with (2) is that you cannot cross-reference the equation number. An alternative approach is to add a caption, and then drag it into the cell of the table (by default, it will initially appear either above or below the table). This will also increment automatically, and you should be able to cross-reference it. However, I have had mixed luck with this approach.
  4. If necessary, add spacing after the paragraph(s) inside the table, so that the equation is vertically centered between the paragraph above and the paragraph below.
  5. To save the resulting table for repeated use, there are (at least) two possibilities. First, you should be able to use Insert -> AutoText -> New... to save the table as an AutoText item. Second, I believe there is something called the Table Gallery that you can add to (this may only be available under Windows). Unfortunately, I have not been able to get either of these to work.

Thursday, August 16, 2012

Caution if updating to mercurial 2.3

The version control system we've been encouraging people to use,  mercurialwas upgraded to version-2.3  this August, 2012.

First - there is no pressing need to upgrade.    If you're curious, or think some of the updates of fixes are pertinent to you, check the release notes.


While the update is fine, several extensions which may affect you may need to be upgraded:

  • hg-git
  • hgsubversion

If you upgrade mercurial to version 2.3, and you experience problems (for example, "hg status" in one of your repositories fails)  check your .hgrc (or _hgrc) file to see if you have either of the above extensions.   If commenting them out solves your problems, then you should upgrade those extensions.

Details on the issue:


Wednesday, July 18, 2012

Initial View settings for a PDF file

To set default magnification and scrolling options for a PDF file, go to File -> Properties -> Initial View, and set it up the way you like it (for a given file). E.g.:






If there are Bookmarks in the file, the layout changes when you click on one of them, relative to the Initial View.  To enforce the same view, select a Bookmark, right click to get Properties, then select Edit under Actions, and change zoom to "Inherit Zoom". Unfortunately there is no built-in way to batch-set all bookmarks to this (unless you want to install something user-written).

Subscribe via email

Enter your email address:

Delivered by FeedBurner

Followers

google analytics