Pages

2011-09-19

R brush for SyntaxHighlighter

(Update, 2013-05-02 14:15 JST: This article is deprecated because I changed the template of this blog.)

I'm working on a custom brush to extend SyntaxHighlighter to support the syntax of R.
The project is on GitHub.
The brush file is SyntaxHighlighter/scripts/shBrushR.js.
Here's an example.

resultframe <- sessionframe <- temp <- bpdata[1,]
transform(resultframe, morningbedtime=c(0))
resultframe <- resultframe[0,]
for (i in 2:length(bpdata$ID)) {
  tempnext <- bpdata[i,]
  if ((abs(difftime(temp$datetime, tempnext$datetime, units="mins")) > 10) || (i == length(bpdata$ID))) {
    if (i == length(bpdata$ID)) {
      sessionframe <- merge(sessionframe, tempnext, all=T)
      sessiondatetime <- strptime(tempnext$datetime, "%Y-%m-%d %H:%M:%S")
    } else {
      sessiondatetime <- strptime(temp$datetime, "%Y-%m-%d %H:%M:%S")
    }
    sessionmax <- mean(sessionframe$max)
    sessionmin <- mean(sessionframe$min)
    sessionbpm <- mean(sessionframe$bpm)
    
    if ((morningbegin <= sessiondatetime$hour) && (sessiondatetime$hour < morningend)) {
      sessionmorningbedtime <- c(0)
    } else {
      sessionmorningbedtime <- c(1)
    }
    
    resultframe <- merge(resultframe, data.frame(datetime=sessiondatetime, max=sessionmax, min=sessionmin, bpm=sessionbpm, morningbedtime=sessionmorningbedtime), all=T)
    sessionframe <- temp <- tempnext
  } else {
    sessionframe <- merge(sessionframe, tempnext, all=T)
    temp <- tempnext
  }
}

2011-08-09

Default settings for bar/column chart axes in major spreadsheet applications may lead to chart junk.

Depending on data, Excel, OpenOffice.org Calc, Google Docs spreadsheet, which are the most popular spreadsheet applications, scale chart axes differently by default.

Below are examples. (twitpic.com)

Note that the vertical axes start from 0, 75, 80 depending on the values in the charts in Excel and OpenOffice.org Calc. Google Docs' chart has its vertical axis from 80, not zero.

The problem is that the mysterious feature may lead to chart junk because the axes don't start from zero and are misleading.

I couldn't find how to fix this by changing default settings with these applications.

So I'm working on Excel macro which scans all the bar/column charts in the current book and make their axes start from zero.

You can download Excel macro book file (FixChartAxes.xlsm) and Excel addin file (FixChartAxes.xlam). (both from Google Docs)

You can see the source code of the macro at Gist.

I think the best solution is that Microsoft fixes this problem, because it is the most influential company in the spreadsheet software industry.

To Microsoft: Please make the default settings of bar/column chart axes start from zero by default.

2011-07-02

R memo: elements' positions in vector under conditions

First, make a vector.
> x <- c(10,20,30)
> x
[1] 10 20 30
If you want to get positions of the elements in vector x which meet a condition, for example, x > 10, you can do like this:
> (1:length(x))[x > 10]
[1] 2 3
The second and third elements meet the condition.

The first part,
> (1:length(x))
[1] 1 2 3
gives you a vector which counts from 1 up to the number of the elements in x.

The last part,
> x > 10
[1] FALSE  TRUE  TRUE
gives you a vector which shows if each element meets the condition (greater than 10).

Then, you can get only the elements which correspond to TRUE by
> (1:length(x))[x > 10]

c.f. 13. ベクトル要素へのアクセス

2011-05-07

MathML test

This post is for testing MathJax on Blogger.

x2-y2

1234

The above examples are generated by Microsoft Mathematics.

The following is from MathML Samples.

x=b±b24ac2a

f(a)=γf(z)zadz

cos(θ+φ)=cos(θ)cos(φ)sin(θ)sin(φ)

D(∇⋅F)dV=DFndS

×F=(FzyFyz)i+(FxzFzx)j+(FyxFxy)k

σ=1Ni=1N(xiμ)2.

(XY)k=Xi(iY)k=Xi(Ykxi+ΓimkYm)

The following is generated by formulator-mathml.

y = ax 2 + bx + c