Create a table from an existing dataframe

We can create from the existing dataframe using table() function

Syntax:

table(dataframe$column_name, dataframe$column_name)

where,

  • dataframe  is the input dataframe
  • column_name is the column names to be created as tables from the dataframe

Example:

In this example, we will be creating a table from an existing data frame using the table function in the R language.

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


Output:

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


How to Create Tables in R?

In this article, we will discuss how to create tables in R Programming Language.

Similar Reads

Method 1: Create a table from scratch

We can create a table by using as.table() function, first we create a table using matrix and then assign it to this method to get the table format....

Method 2: Create a table from an existing dataframe

...