Error in cool_function[1] : object of type ‘closure‘ is not subsettable

Posted Data+Science+Insight

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Error in cool_function[1] : object of type ‘closure‘ is not subsettable相关的知识,希望对你有一定的参考价值。

Error in cool_function[1] : object of type 'closure' is not subsettable

目录

Error in cool_function[1] : object of type 'closure' is not subsettable

问题:

解决:

完整错误:


问题:

#函数怎么能被索引那,函数计算返回的结果有可能是可以索引的。

#define function
cool_function <- function(x) 
  x <- x*5
  return(x)



#define data
data <- c(2, 3, 3, 4, 5, 5, 6, 9)

#apply function to data
cool_function(data)

#attempt to get first element of function
cool_function[1]

#其他类似错误

#attempt to subset mean function
mean[1]

Error in mean[1] : object of type 'closure' is not subsettable

#attempt to subset standard deviation function
sd[1]

Error in sd[1] : object of type 'closure' is not subsettable

#attempt to subset table function
tabld[1]

Error in table[1] : object of type 'closure' is not subsettable

解决:

#typeof获取数据类型

#print object type of function
typeof(cool_function)

[1] "closure"

#正确的操作

#apply function to just first element in vector
cool_function(data[1])


#apply function to every element in vector
cool_function(data)

完整错误:

> #define function
> cool_function <- function(x)
+     x <- x*5
+     return(x)
+


> #define data
> data <- c(2, 3, 3, 4, 5, 5, 6, 9)

> #apply function to data
> cool_function(data)
[1] 10 15 15 20 25 25 30 45

> #attempt to get first element of function
> cool_function[1]
Error in cool_function[1] : object of type 'closure' is not subsettable

以上是关于Error in cool_function[1] : object of type ‘closure‘ is not subsettable的主要内容,如果未能解决你的问题,请参考以下文章

Gradle build finished with 1 error(s) in 15s 197m

dbType postgresql, , druid-version 1.2.5, syntax error: syntax error, error in :‘desc LIMIT ?,?,

error: stray '377' in program

LINK : fatal error LNK1117: syntax error in option 'VERSION:1.6.5'

ParserError: Error tokenizing data. C error: Expected 1 fields in line 122, saw 2

Server Error in '/' Application