meineko’s blog

元つくばの某独立行政法人勤務の植物屋です。最近は、ほぼ、突発天体の話題です。

TAO Survey

やっぱり、Rは、簡単でいいなぁ。

library(RMySQL)
dbconnector <- dbConnect(dbDriver("MySQL"), dbname="SurveyDB", user="meineko", password = "password")
test.table <- dbGetQuery(dbconnector, "select frame_id, obs_date_ut, magzero, inst_filter from Frame_Info")
dbDisconnect(dbconnector)

で、frame_idが、8,9,15のデータを取り出す。

>x <- c(8, 9 ,15)
>test.table[test.table["frame_id"] == x, ]
  frame_id         obs_date_ut  magzero inst_filter
1        8 2010-11-04 15:57:45 17.51892           V
2        9 2010-11-05 16:09:33 18.36223           V

あれ??
だめだ、わからん。

> x <- c(8, 10, 25)
> test.table[test.table["frame_id"] == x, ]
   frame_id         obs_date_ut  magzero inst_filter
1         8 2010-11-04 15:57:45 17.51892           V
18       25 2010-12-16 13:29:50 18.03625           V
> x <- c(10)
> test.table[test.table["frame_id"] == x, ]
  frame_id         obs_date_ut  magzero inst_filter
3       10 2010-11-09 15:58:25 18.67189           V

え?


(追記)

frameID <- function(x){test.table[test.table$frame_id == x, ]}

関数化して、sapplyしたら出来ましたが、今度は、結果のタテヨコが逆です。
data.frame(数字と文字が混在)なので、簡単には、縦と横が入れ替えられません。
なんとも?


(追記2)

>id <- as.matrix(r[1, ])
>date_obs <- as.matrix(r[2, ])
>magzero <- as.matrix(r[3, ])
> d <- cbind(id, date_obs, magzero)
> d
     [,1] [,2]                  [,3]    
[1,] 10   "2010-11-09 15:58:25" 18.67189
[2,] 15   "2010-11-10 15:50:57" 17.78286
[3,] 25   "2010-12-16 13:29:50" 18.03625
[4,] 45   "2010-11-18 15:10:32" 19.4766 
> 

みたいに切り出して、つなぎ変えたら出来ました。
なんか、スマートでないな?