Yaleで、遊んで学ぶ日々。

Yaleで、遊んで学ぶ日々。

囲碁、ときどきプログラミング、ところにより経済。
[1]  [2]  [3]  [4]  [5]  [6
Matlabプログラミングの問題が出されて、答えを書くとその長さに応じて得点が入る。短いほど高得点らしい。
http://www.mathworks.com/matlabcentral/cody/


今日はこれを考えてみる(ソース)。

INSTRUCTIONS

This is a multi-dimensional variant of the normal multiplication table used to teach elementary students multiplication. In this variant, we are going to produce a table that multiplies the divisors 1 to n (input) agains itself in d dimensions.

Note: Inputting d = 0 should return the number 1 and d = 1 should return a column vector with the elements 1 to n.

Example:

Input:

n = 3;
d = 3;

Output:

tt(:,:,1) = [ 1  2  3
              2  4  6
              3  6  9  ];
tt(:,:,2) = [ 2  4  6
              4  8  12
              6  12 18 ];
tt(:,:,3) = [ 3  6  9
              6  12 18
              9  18 27 ];

データフレームに長い名前を付けてしまうと、いちいち名前を指定するのが面倒くさい。

data.with.long.name <- data.frame(gdp=runif(10), price=runif(10))
data.with.long.name$gdppc <- data.with.long.name$gdp / data.with.long.name$price


with() を使うと、少しだけ楽になる。
data.with.long.name <- data.frame(gdp=runif(10), price=runif(10))
data.with.long.name$gdppc <- with(data.with.long.name, gdp / price)


within() というのもあって少し使いかたが違う。
data.with.long.name <- data.frame(gdp=runif(10), price=runif(10))
data.with.long.name <- within(data.with.long.name, gdppc <- gdp / price)


命令は複数書いても良いから、結構便利。


大した問題でもないが、pmlパッケージのpdata.frame関数に不具合発見。

どうも、「非欠損値が1つしかない」列があると困るらしい。
せっかくなのでcranプロジェクトにバグレポートしてみたら、これは「バグ」ではないとのこと。難しいもんだな。


library(plm)
 
dat <- data.frame(i = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4),
                  t = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3))
dat$x <- NA
dat$y <- NA
dat$z <- NA
dat$y[1] <- 1
dat$z[1:2] <- 2:3
 
dat
 
# this fails
pdata.frame(dat, index = c("i", "t"))
 
# this is fine
pdata.frame(dat[, -4], index = c("i", "t"))

 

sessionInfo()

以上だそうで。

ソース:http://r.789695.n4.nabble.com/Determine-package-version-in-R-td870896.html
 

出力例
> sessionInfo()
 
R version 2.13.2 (2011-09-30)
Platform: x86_64-pc-mingw32/x64 (64-bit)
 
locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    
 
attached base packages:
[1] splines   grid      stats     graphics  grDevices utils    
[7] datasets  methods   base     
 
other attached packages:
 [1] plm_1.2-8         nlme_3.1-102      bdsmatrix_1.3    
 [4] foreign_0.8-46    AER_1.1-9         strucchange_1.4-6
 [7] sandwich_2.2-9    lmtest_0.9-29     zoo_1.7-6        
[10] Formula_1.0-1     car_2.0-11        survival_2.36-9  
[13] nnet_7.3-1        MASS_7.3-14       ggplot2_0.8.9    
[16] proto_0.3-9.2     reshape_0.8.4     plyr_1.6         
 
loaded via a namespace (and not attached):
[1] lattice_0.19-33 tools_2.13.2   
 
Counter
Calender
12 2012/01 02
S M T W T F S
1 3 5 6 7
8 9 10 11 12 13
15 16 17 19 20 21
22 28
29 30 31
Search in This Blog
Latest Comments
[01/26 私だ]
[01/26 わかりません]
[01/21 Ovamy]
[01/14 maync]
[11/17 kohta]
Latest Trackbacks
Oldest Posts
About Me
HN:
kotakun
年齢:
28
性別:
男性
誕生日:
1983/10/01
職業:
学生
フリーエリア




Barcode
Access Analysis
Powerd by NINJAブログ / Designed by SUSH
Copyright © Yaleで、遊んで学ぶ日々。 All Rights Reserved.
ブログ [PR]帽子 オフィスビル