Comment créer des tables dans R ?
Dans cet article, nous verrons comment créer des tables dans le langage de programmation R.
Méthode 1 : créer un tableau à partir de zéro
Nous pouvons créer un tableau en utilisant la fonction as.table(), nous créons d’abord un tableau en utilisant une matrice, puis l’attribuons à cette méthode pour obtenir le format du tableau.
Syntaxe :
as.table(data)
Exemple:
Dans cet exemple, nous allons créer une matrice et l'attribuer à une table en langage 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> |
Sortir:
col1 col2 col3 col4 row1 1 2 3 4 row2 5 6 7 8 row3 9 10 11 12 row4 13 14 15 16
Méthode 2 : créer une table à partir d’un dataframe existant
Nous pouvons créer à partir du dataframe existant en utilisant 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> |
Sortir:
5 6 7 8 1 1 0 0 0 2 0 1 0 0 3 0 0 1 0 4 0 0 0 1