【发布时间】: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