library(sqldf) #省略名と値のデータを作成 Abbr <- data.frame( Species = rep(levels(iris$Species),3) ,Abbr = rep(c("S", "Ve", "Vi"),3) ,Petal.Width = c(rep(0.1,3),rep(1.5,3),rep(2,3)) ) str(iris) #(なぜか.が_になるのを無視して以下のsql式を実行)#列名AbbrとPetal.widthとSepal.Lengthの平均を出力 #データはirisとAbbrのnaturaljoinする。 #結合はSpecies,Petal_Widthでグルーピングされる。 sqldf( "select Abbr,Petal_Width,avg(Sepal_Length) from iris natural join Abbr group by Species,Petal_Width" ) #natural joinは一番楽な方法だが共通する列名について既知でないちいけない。列名はselect *としてやると全部一覧される。 #また、単に結合するのであれば、selectに*を使って全部表示するようにする。 sqldf( "select Abbr.*,iris.* from Abbr inner join iris on iris.Species = Abbr.Species" )