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
 > 










