20 C
London
Wednesday, July 23, 2025
HomeRFix Common Errors in RHow to Fix in R: ‘height’ must be a vector or a...

How to Fix in R: ‘height’ must be a vector or a matrix

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

One error you may encounter in R is:

Error in barplot.default(df) : 'height' must be a vector or a matrix

This error occurs when you attempt to use the barplot() function to create a bar plot in R, yet you provide the name of a data frame instead of the name of a column within the data frame.

This tutorial shares exactly how to fix this error.

How to Reproduce the Error

Suppose we have the following data frame in R:

#create data frame
df frame(player=c('A', 'B', 'C', 'D', 'E'),
                 points=c(17, 12, 8, 9, 25))

#view data frame
df

  player points
1      A     17
2      B     12
3      C      8
4      D      9
5      E     25

Now suppose we attempt to use the barplot() function to create a bar plot:

#attempt to create bar plot
barplot(df)

Error in barplot.default(df) : 'height' must be a vector or a matrix

We receive an error because we provided the name of a data frame in the barplot() function instead of the name of a data frame column.

How to Fix the Error

The easiest way to fix this error is to simply provide a name of a data frame column to the barplot() function:

#create bar plot to visualize values in points column
barplot(df$points)

Notice that we don’t receive any error this time since we provided the name of a data frame column to the barplot() function.

Also note that we can use the following syntax to add axis labels to the plot to make it easier to interpret:

#create bar plot with labels
barplot(df$points, names=df$player, xlab='Player', ylab='Points')

The x-axis displays the player names while the y-axis displays the points values for each player.

Additional Resources

The following tutorials explain how to fix other common errors in R:

How to Fix in R: NAs Introduced by Coercion
How to Fix in R: Subscript out of bounds
How to Fix in R: longer object length is not a multiple of shorter object length
How to Fix in R: number of items to replace is not a multiple of replacement length

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories