読者です 読者をやめる 読者になる 読者になる

run_r.rb

Ruby 1000speakers

friend.rbの情報から動的にRの変数を作成させるためのRubyのコード。別に手でRのコード書いてもいいけど、面倒なので自動化した。変数になりそうなもの全部をregに入れておいてもいいけど、2^nで考えないといけないことが増えるので効きそうなものを放りこんでいった。

こんな感じの規則的なデータが生成される。

names(twitter) <- c("name","is_follow","following","followers","favorites","update","time_diff","time_mean","entry_size","is_japanese","is_tsukuba","is_friend","is_tokyo","is_blog_in_web","is_blog_in_bio","is_otaku","is_students","is_program","at_mark","is_program_in_blog","is_otaku_in_blog","is_students_in_blog","is_program_by_bayes","is_otaku_in_bio_by_bayes","is_otaku_in_entry_by_bayes")

twit <- c("following","followers","favorites","update","per","entry_size","is_students","is_program","is_friend","is_program_by_bayes","at_mark","is_japanese","time_mean")

grid <- expand.grid(following = c(0,1),followers = c(0,1),favorites = c(0,1),update = c(0,1),per = c(0,1),entry_size = c(0,1),is_students = c(0,1),is_program = c(0,1),is_friend = c(0,1),is_program_by_bayes = c(0,1),at_mark = c(0,1),is_japanese = c(0,1),time_mean= c(0,1))
#!/usr/bin/ruby

ruby_file_name = "friend.rb"
r_file_name = "twitter.R"

r_file = File.open(r_file_name,'w')
ruby_file = File.open(ruby_file_name,'r')

label = []
while text = ruby_file.gets do
  if(/##(.*)/ =~ text)
    label.push $1
  end
end

names = label.join('","')

reg = ["following","followers","favorites","update","per","entry_size","is_students","is_program","is_friend","is_program_by_bayes","at_mark","is_japanese","time_mean"]

twit = reg.join('","')
grid = reg.join(' = c(0,1),')

r_file.puts <<EOS

names(twitter) <- c("#{names}")

twit <- c("#{twit}")

grid <- expand.grid(#{grid}= c(0,1))
EOS

ruby_file.close
r_file.close