【问题标题】:R language: object of type 'closure' is not subsettable while applying [ with lapplyR 语言:“闭包”类型的对象在应用 [ 与 lapply 时不可子集化
【发布时间】:2019-07-09 17:33:35
【问题描述】:
Weather <- list    (Chicago=chicagoF,Houston=houstonF,
NewYork=NewYorkF,SanFrancisco=SanFranciscoF)


chicagoF which is one of the list items is as below
(temperature is in Farenheit; hence F after Chicago) 

                  Jan    Feb    Mar    Apr    May    Jun    Jul        
AvgHigh_F        32.00  36.00  46.00  59.00  70.00  81.00  84.00    
AvgLow_F         18.00  21.00  30.00  41.00  52.00  63.00  68.00  
AvgPrecip_inch    2.05   1.93   2.72   3.62   4.13   4.06   4.02      
DaysWithPrecip   10.00   8.00  11.00  11.00  11.00  10.00   9.00      
HoursOfSunshine 135.00 136.00 187.00 215.00 281.00 311.00 318.00  

类似地,其他 3 个城市旧金山、休斯顿和纽约(列表中的其他 3 个项目)具有相同的列和不同的数据。

我正在尝试使用 Lapply 复制 Weather$Chicago[1,1]。如果我 不要使用 Lapply,那么我需要输入 4 个命令:

4 steps I want to implement:
Weather$Chicago[1,1] #It should result in 32 for example
Weather$Houston[1,1] # similarly accessing first element of the next
item in list
Weather$NewYork[1,1]
Weather$SanFrancisco[1,1]

现在这 4 个步骤是迭代的,因此我希望使用 lapply 其中第一项是列表名称 = 天气,第二项 = [ 或括号运算符,第三个元素和第四个元素是 1 和 1,这与访问相同单个列表项 Chicago、Houston 等的第一个元素:Weather$Chicago[1,1]

尝试lapply[Weather, "[", 1,1] 会出错。

lapply[Weather, "[", 1, 1] 中的错误:'closure' 类型的对象不是 子表****

请告诉如何使用 lapply 实现此列表的 4 个步骤。

【问题讨论】:

  • 我认为这样做更容易:lapply(lst,function(x) x[1,2]).

标签: r


【解决方案1】:

我们可以使用以下内容,其中lst 是您的数据框列表:

lapply(lst,"[",i=1,j=1)

例子:

lst <- list(iris,mtcars)
lapply(lst,"[",i=1,j=2)
[[1]]
[1] 3.5

[[2]]

[1] 6

【讨论】:

  • 您好,感谢您的帮助,但错误仍然存​​在。 lapply[Weather, "[", i = 1, j = 1] 中的错误:'closure' 类型的对象不是子集我尝试使用 I=1, j=2 但同样的错误仍然存​​在。不可子集
【解决方案2】:

感谢您的帮助。我输入代码时出错。我用 [ 代替 ( 括号

所以我用了

 lapply[Weather,"[",1,1].

我一使用圆括号,问题就解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 2019-03-23
    • 2019-10-18
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多