Create Data Frame of Unequal Lengths in R (Example)

 

In this article you’ll learn how to create a data frame with columns that have an unequal length in the R programming language.

Table of contents:

Here’s the step-by-step process:

 

Creation of Example Data

First, I’ll have to create some example data:

x1 <- 1:8                                       # Create first example variable
x1                                              # Print first example variable
# [1] 1 2 3 4 5 6 7 8

The previous output of the RStudio console shows the structure of our first vector (i.e. data frame variable). This first vector contains eight vector elements.

Let’s create another variable:

x2 <- letters[1:5]                              # Create second example variable
x2                                              # Print second example variable
# [1] "a" "b" "c" "d" "e"

Our second vector consists of only five elements, i.e. the length is different compared to the first vector.

 

Example: Create Data Frame of Unequal Lengths by Adding NA at Bottom

In this example, I’ll illustrate how to create a data frame with unequal lengths of the columns by using the length() and max() functions in R.

First, we can apply the length and max functions to return the maximum length of our vector objects:

max_length <- max(c(length(x1), length(x2)))    # Find out maximum length
max_length                                      # Print maximum length
# [1] 8

After running the previous R code, the RStudio console returns the maximum length of our variables (i.e. 8).

Now, we can use this information to create a data frame with NA values at the bottom of each column that is shorter as the maximum length.

Consider the following R syntax:

data <- data.frame(col1 = c(x1,                 # Create data frame with unequal vectors
                            rep(NA, max_length - length(x1))),
                   col2 = c(x2,
                            rep(NA, max_length - length(x2))))
data                                            # Print final data frame

 

table 1 data frame create data frame unequal lengths r

 

After executing the previous code, we have created Table 1, i.e. a data frame containing the values of our two example vectors. As you can see, the second column has NA values at the bottom.

 

Video & Further Resources

Have a look at the following video of my YouTube channel. In the video, I illustrate the topics of this article in a live session:

 

The YouTube video will be added soon.

 

Furthermore, you may want to read some of the related articles on this homepage:

 

Summary: In this article, I have illustrated how to construct a data frame where the variables have a different or uneven length in the R programming language. Please let me know in the comments section, in case you have additional comments and/or questions.

 

Subscribe to my free statistics newsletter

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


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.

Menu
Top