noob
noob

Reputation: 3811

R language: object of type 'closure' is not subsettable while applying [ with lapply

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  

Similarly the other 3 cities SanFrancisco, Houston and New York (other 3 items of the list) have the same columns and different data.

I am trying to replicate Weather$Chicago[1,1] by using Lapply. If I don't use Lapply then I need to type 4 commands:

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]

Now these 4 steps are iterative, hence I wish to use lapply where first item is list name=Weather, second item = [ or the bracket operator and 3rd element and 4th element are 1 and 1 which is same as accessing individual list items Chicago, Houston etc's first elements: Weather$Chicago[1,1]

Trying lapply[Weather, "[", 1,1] is giving error.

Error in lapply[Weather, "[", 1, 1] : object of type 'closure' is not subsettable****

Please tell how to implement the 4 steps of this list by using lapply.

Upvotes: 2

Views: 102

Answers (2)

noob
noob

Reputation: 3811

Thanks for the help. There was an error in my typing the code. I used [ instead of ( bracket

So I used

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

As soon as I used round brackets, the problem got solved.

Upvotes: 0

NelsonGon
NelsonGon

Reputation: 13319

We can use the following where lst is your list of data frames:

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

Example:

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

[[2]]

[1] 6

Upvotes: 3

Related Questions