Rでテーブルを作成するには?
この記事では、R プログラミング言語でテーブルを作成する方法について説明します。
方法 1: テーブルを最初から作成する
as.table() 関数を使用してテーブルを作成できます。まず行列を使用してテーブルを作成し、それをこのメソッドに割り当ててテーブル形式を取得します。
構文 :
as.table(data)
例:
この例では、R 言語で行列を作成し、テーブルに割り当てます。
R
# create matrix with 4 columns and 4 rows> data=> matrix> (> c> (1:16), ncol=4, byrow=> TRUE> )> # specify the column names and row names of matrix> colnames> (data) => c> (> 'col1'> ,> 'col2'> ,> 'col3'> ,> 'col4'> )> rownames> (data) <-> c> (> 'row1'> ,> 'row2'> ,> 'row3'> ,> 'row4'> )> # assign to table> final=> as.table> (data)> # display> final> |
出力:
col1 col2 col3 col4 row1 1 2 3 4 row2 5 6 7 8 row3 9 10 11 12 row4 13 14 15 16
方法 2: 既存のデータフレームからテーブルを作成する
次を使用して既存のデータフレームから作成できます R
R
# create dataframe with 4 columns and 4 rows> data=> data.frame> (col1=> c> (1:4),col2=> c> (5:8),> > col3=> c> (9:12),col4=> c> (13:16))> # assign to table from dataframe> final=> table> (data$col1,data$col2)> # display> final> |
出力:
5 6 7 8 1 1 0 0 0 2 0 1 0 0 3 0 0 1 0 4 0 0 0 1