Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

How to fix in r?

3 Answer(s) Available
Answer # 1 #

One common error you may encounter in R is:

This error usually occurs for one of two reasons:

Reason 1: You are attempting to reference an object you have not created.

Reason 2: You are running a chunk of code where the object has not been defined in that chunk.

The following examples how to resolve this error in each of these scenarios.

Suppose we use the following code to display a data frame that we have not created:

We receive an error because the object my_data does not exist.

Instead, we need to type the correct name of the data frame that we created:

This time we’re able to display the data frame without an error because we used the correct name.

Note that we can also use ls() to display all object names in our current environment and exists() to check if a specific object name exists:

We can see that exists(‘my_data’) returns FALSE, which explains why we received an error when we attempted to display it.

Another reason that we might receive an object not found error is because we have highlighted a chunk of code to run in RStudio that doesn’t contain the name of the object we’re attempting to reference.

For example, consider the following screenshot where we highlight rows 3 through 5 and attempt to calculate the mean of a value named x:

Since we created the vector named x in row 2, we receive an error because we haven’t actually created that vector in the chunk of code that we highlighted.

If we instead make sure that we highlight the whole chunk of code we’re interested in, we won’t receive any error:

[3]
Edit
Query
Report
Shahida kak
CONCRETE FENCE BUILDER
Answer # 2 #
  • Run outside of RStudio. Test your R code by running it through the same version of R on a standard console session (RGui, R.
  • Search for help.
  • Problems with a certain R function or topic.
[3]
Edit
Query
Report
Mehar Balagopalan
BAND AND CUFF CUTTER
Answer # 3 #

There are several data types in R, the main ones being:

You know that some operations and analyses are possible and appropriate only with some specific types of data.

For example, it is not appropriate to compute the mean of a factor or character variable:

Likewise, although it is technically possible, it makes little sense to draw a barplot of a quantitative continuous variable because in most cases, the frequency will be 1 for each value:

(By the way, if your data is not already displayed in the form of a table, do not forget to add table() inside the barplot() function.)

[2]
Edit
Query
Report
Bronwen Cassell
Social Science