This error occurs when you try to use a list to subset a data frame. You can solve this error by using the c
function to subset the data frame based on a vector. For example,
df_subset <- df[c("x", "z")] df_subset
This tutorial will go through the error in detail and how to solve it with code examples.
Example
Let’s look at an example to reproduce the error.
# Create a data frame with three columns df <- data.frame(x = rnorm(10), y = letters[1:10], z = 1:10) df
x y z 1 -0.38117846 a 1 2 0.90089817 b 2 3 -0.06359607 c 3 4 0.07582160 d 4 5 0.98323210 e 5 6 1.91295945 f 6 7 -1.56388794 g 7 8 0.93328058 h 8 9 -0.75076193 i 9 10 1.44142624 j 10
Next, we will try to subset the data frame using a list with two column names.
# Attempt to subset data frame using list df_subset <- df[list("x", "z")]
Let’s run the code to see what happens:
Error in `[.default`(df, list("x", "z")) : invalid subscript type 'list'
The error occurs because we cannot subset a data frame with a list in R.
Solution
We can solve the error by switching out the list function with the c
function. We need to use a vector to subset the data frame instead of a list
. Let’s look at the revised code:
df_subset <- df[c("x", "z")] df_subset
Let’s run the code to get the result:
x z 1 -0.38117846 1 2 0.90089817 2 3 -0.06359607 3 4 0.07582160 4 5 0.98323210 5 6 1.91295945 6 7 -1.56388794 7 8 0.93328058 8 9 -0.75076193 9 10 1.44142624 10
We successfully created a new data frame which is a subset of the original data frame.
Summary
Congratulations on reading to the end of this tutorial!
For further reading on R-related errors, go to the articles:
- How to Solve R Error in sort.int(x, na.last = na.last, decreasing = decreasing, …) : ‘x’ must be atomic
- How to Solve R Error as.Date.numeric(x) : ‘origin’ must be supplied
- How to Solve R Error: invalid (do_set) left-hand side to assignment
Go to the online courses page on R to learn more about coding in R for data science and machine learning.
Have fun and happy researching!
Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!