Object of type 'closure' is not subsettable

Hi everyone,

I run the code below:

#Create a Total column to show how much customer spent on each purchase:

data$Total <- data$Quantity*data$UnitPrice

An error comes: Error in data$Quantity : object of type 'closure' is not subsettable

I guess that the problems comes because I used data$something

Please help me how to fix the problem! :cry:
Thank you so much!

Check what type of object data is by doing str(data)

if it's a data frame your syntax should work:

df <- data.frame(v1 = c(4:6), 
                 v2 = c(1:3))

df$v3 <- df$v1 * df$v2
#>   v1 v2 v3
#> 1  4  1  4
#> 2  5  2 10
#> 3  6  3 18

Created on 2019-01-28 by the reprex package (v0.2.1)

I am suspicious you either don't have a data frame or you have one that is malformed in some way.

After you get your data issues sorted, you may also be interested in exploring dplyr as it makes the syntax for data frame transforms much simpler:

#> Warning: package 'tibble' was built under R version 3.5.2
df <- data.frame(v1 = c(4:6), 
                 v2 = c(1:3))
df %>%
  mutate(v3 = v1 * v2)
#>   v1 v2 v3
#> 1  4  1  4
#> 2  5  2 10
#> 3  6  3 18

Created on 2019-01-28 by the reprex package (v0.2.1)


Make sure you have created a data variable prior to running the line of code you shared.

In a clean R session try the following,

# function (..., list = character(), package = NULL, lib.loc = NULL, 
#    verbose = getOption("verbose"), envir = .GlobalEnv) 
# {
#    fileExt <- function(x) {
#      db <- grepl("\\.[^.]+\\.(gz|bz2|xz)$", x)
# .. (and more)

# Error in data$foo : object of type 'closure' is not subsettable

There is a function data() in the base utils package, which is loaded by default. An error is raised because right now we are asking R to extract a value from the data function as though it were a list.

Now let's create a data object. Once this new variable is created R will automatically distinguish the data object variable from the data function variable.

data <- list()
data$field <- "value"
# $field
# [1] "value"

Because R, for better or worse, allows this silent distinction detailed variable names are often a safer approach. I hope this helps.


Dear @jdlong and @nteetor,

Thank you so much for your help,

Your advice helps me a lot and finally my problem is solved! :grinning:

Thank you!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.