Cum se creează tabele în R?
În acest articol, vom discuta despre cum să creați tabele în limbajul de programare R.
Metoda 1: Creați un tabel de la zero
Putem crea un tabel folosind funcția as.table(), mai întâi creăm un tabel folosind matrice și apoi îl atribuim acestei metode pentru a obține formatul tabelului.
Sintaxă :
as.table(data)
Exemplu:
În acest exemplu, vom crea o matrice și o vom atribui unui tabel în limbajul 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> |
Ieșire:
col1 col2 col3 col4 row1 1 2 3 4 row2 5 6 7 8 row3 9 10 11 12 row4 13 14 15 16
Metoda 2: Creați un tabel dintr-un cadru de date existent
Putem crea din cadrul de date existent folosind 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> |
Ieșire:
5 6 7 8 1 1 0 0 0 2 0 1 0 0 3 0 0 1 0 4 0 0 0 1