Kā izveidot tabulas R?

Šajā rakstā mēs apspriedīsim, kā izveidot tabulas R programmēšanas valodā.

1. metode: izveidojiet tabulu no nulles

Mēs varam izveidot tabulu, izmantojot funkciju as.table(), vispirms mēs izveidojam tabulu, izmantojot matricu, un pēc tam piešķiram to šai metodei, lai iegūtu tabulas formātu.

Sintakse :

as.table(data) 

Piemērs:

Šajā piemērā mēs izveidosim matricu un piešķirsim to tabulai R valodā.

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>

Izvade:

 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. metode: izveidojiet tabulu no esoša datu rāmja

Mēs varam izveidot no esošā datu rāmja, izmantojot 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>

Izvade:

 5 6 7 8 1 1 0 0 0 2 0 1 0 0 3 0 0 1 0 4 0 0 0 1