R Error in Ops.data.frame() : only defined for equally-sized data frames (2 Examples)

In this R tutorial you’ll learn how to deal with the “Error in Ops.data.frame() : only defined for equally-sized data frames”.

Example Data

set.seed(928546367)                          # Setting a seed
df_A <- round(data.frame(col1 = rnorm(5),    # Creating two data frames
                         col2 = rnorm(5)), 1)
df_A
#   col1 col2
# 1  0.5  1.2
# 2  2.6 -0.1
# 3 -1.4  0.4
# 4 -1.3 -2.0
# 5  0.0  0.1
df_B <- round(data.frame(col1 = rnorm(5),
                         col2 = rnorm(5),
                         col3 = rnorm(5)), 1)
df_B
#   col1 col2 col3
# 1 -0.9 -0.5 -0.9
# 2 -0.1  0.6 -0.8
# 3 -0.5  0.4  1.0
# 4  2.0  1.3 -1.2
# 5 -1.0 -0.7 -0.4

Example 1: Replicating the Error Message in Ops.data.frame() – only defined for equally-sized data frames

df_A + df_B                                  # Trying to add values in data frames
# Error in Ops.data.frame(df_A, df_B) : 
#   '+' only defined for equally-sized data frames

Example 2: Debugging the Error Message in Ops.data.frame() – only defined for equally-sized data frames

df_A + df_B[ , colnames(df_A)]               # Using data frames with same structure
#   col1 col2
# 1 -0.4  0.7
# 2  2.5  0.5
# 3 -1.9  0.8
# 4  0.7 -0.7
# 5 -1.0 -0.6

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top