Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

$ operator is invalid for atomic vectors in R after using data.frame()

I have read several posts about this problem in r and I think I have understood the reason behind it. However, after adjusting, my code still shows an error message. This code is based on what my friend told me.

get_ETA <- function(lineId, stationName) {
  path_1 <- paste0("https://api.tfl.gov.uk/Line/", lineId, "/arrivals")
  response_1 <- GET(path_1)
  content_1 <- data.frame(content(response_1))
  if (status_code(response_1) >= 400) stop("Error:typo in line Id")
  for (i in 1:length(content_1)) {
    if (content_1[[i]]$stationName == stationName) break
  }
  naptanId <- content_1[[i]]$naptanId
  path_2 <- paste0("https://api.tfl.gov.uk/Line/", lineId, "/arrivals", naptanId)
  response_2 <- GET(path_2)
  content_2 <- data.frame(content(response_2))
  eta <- c()
  for (i in 1:length(content_2)) {
    eta[i] <- content_2[[i]]$expectedArrival
  }
  return(sort(eta)[1])
}

And the error is 'Error: $ operator is invalid for atomic vectors'.

When I try some individual code, for instance

e <- GET("https://api.tfl.gov.uk/Line/northern/arrivals")
response <- content(e)
response[[2]]$stationName

There seems to be no such an error. Could someone help me fix it?

question from:https://stackoverflow.com/questions/65844305/operator-is-invalid-for-atomic-vectors-in-r-after-using-data-frame

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It seems a crucial term is missing on the ninth line.

 naptanId <- content_1$matches[[i]]$naptanId

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...