Create Data Frame with n Rows & m Columns in R (Example)

 

In this R tutorial you’ll learn how to initialize an empty data frame with particular dimensions.

Table of contents:

Let’s dive into it.

 

Example: Create Empty Data Frame with Certain Dimensions

This example shows how to construct a data frame containing n rows and m variables.

For this, we can use the data.frame function in combination with the matrix function.

Within the matrix function, we have to specify the number of rows using the nrow argument and the number of columns using the ncol argument.

data <- data.frame(matrix(NA,    # Create empty data frame
                          nrow = 5,
                          ncol = 4))
data                             # Print empty data frame

 

table 1 data frame create data frame n rows and m columns r

 

Have a look at Table 1. It illustrates the RStudio console output of the previous R code.

As you can see, our data frame has five rows and two columns. All data cells have been set to NA.

 

Video & Further Resources

Some time ago I have released a video on my YouTube channel, which illustrates the R codes of this article. You can find the video below.

 

 

Furthermore, I can recommend reading the other articles of my website. I have released several other articles on related topics such as loops, matrices, and extracting data:

 

This article showed how to construct a data frame having n rows and m columns in the R programming language. In case you have additional questions, let me know in the comments section below. Furthermore, please subscribe to my email newsletter to receive updates on new articles.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


4 Comments. Leave new

  • Mohamed Nasr
    May 3, 2023 9:51 pm

    Hello,

    I have csv file of large number of columns and rows and I want to do a dataframe of this file. How can I do it.

    Kind regards,

    Reply
    • Hello Mohamed,

      You can use the tidyverse package for importing a CSV file. If it has not been installed yet, first install it as follows.

      install.packages(tidyverse)

      Then you can follow the next steps. You should first copy the file path of your csv file. Then you can save the data as shown below.

      library(tidyverse)
       
      file_path <- "C:/Users/Admin/Desktop/Pizza.csv" # Replace this with the path to your CSV file
      data <- read.csv(file_path) #define data
       
      head(data) #print first few rows
      #   brand    id  mois  prot   fat  ash sodium carb  cal
      # 1     A 14069 27.82 21.43 44.87 5.11   1.77 0.77 4.93
      # 2     A 14053 28.49 21.26 43.89 5.34   1.79 1.02 4.84
      # 3     A 14025 28.35 19.99 45.78 5.08   1.63 0.80 4.95
      # 4     A 14016 30.55 20.15 43.13 4.79   1.61 1.38 4.74
      # 5     A 14005 30.49 21.28 41.65 4.82   1.64 1.76 4.67
      # 6     A 14075 31.14 20.23 42.31 4.92   1.65 1.40 4.67

      You are now ready to use your data in your analysis. It is successfully imported.

      Regards,
      Cansu

      Reply
  • Mohamed Nasr
    May 4, 2023 8:15 am

    Hello, I have already imported the file. My file has 140 columns and 50 rows all of them have different values. I want to know how to do dataframe of the file after after importing the file.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top