【发布时间】:2021-04-27 14:49:27
【问题描述】:
所以这应该是一个将列表中的项目拉入数据框的相对简单的问题,但我遇到了一些问题。
我有以下列表(我只是为您显示列表的一部分,它比这长得多):
str(raw_jobs_list)
List of 2
$ :List of 4
..$ id : chr "3594134"
..$ score : int 1
..$ fields:List of 16
.. ..$ date :List of 3
.. .. ..$ changed: chr "2020-04-18T00:35:00+00:00"
.. .. ..$ created: chr "2020-04-07T11:15:37+00:00"
.. .. ..$ closing: chr "2020-04-17T00:00:00+00:00"
.. ..$ country :List of 1
.. .. ..$ :List of 6
.. .. .. ..$ href : chr "https://api.reliefweb.int/v1/countries/149"
.. .. .. ..$ name : chr "Mali"
.. .. .. ..$ location :List of 2
.. .. .. .. ..$ lon: num -1.25
.. .. .. .. ..$ lat: num 17.4
.. .. .. ..$ id : int 149
.. .. .. ..$ shortname: chr "Mali"
.. .. .. ..$ iso3 : chr "mli"
.. ..$ title : chr "REGIONAL MANAGER West Africa"
我尝试使用以下方法将它们拉出:
jobs_data_df <- list.stack(list.select(raw_jobs_list,
fields$title,
fields$country$name,
fields$date$created))
raw_jobs_list 是列表,但我得到了这些 NA,但不知道如何通过它。
glimpse(jobs_data_df)
Rows: 2
Columns: 3
$ V1 <chr> "REGIONAL MANAGER West Africa", "Support Relief Group Public Health Advisor (Multiple Positions)"
$ V2 <lgl> NA, NA
$ V3 <chr> "2020-04-07T11:15:37+00:00", "2020-05-04T15:20:37+00:00"
我可能忽略了一些明显的东西,因为我以前没有过多地使用列表。有什么想法吗?
非常感谢! C
PS。如果你有兴趣,我正在使用这个 API,这就是我到目前为止的方式。
jobs <- GET(url = "https://api.reliefweb.int/v1/jobs?appname=apidoc&preset=analysis&profile=full&limit=2")
raw_jobs_list <- content(jobs)$data
上面显示的部分是整个数据的一个子集;这是列表的第一个元素的一部分:
dput(lapply(raw_jobs_list, function(x) c(x[c("id","score")], list(fields=x[[3]][intersect(names(x[[3]]),c("date","country","title"))]))))
list(list(id = "3594134", score = 1L, fields = list(date = list(
changed = "2020-04-18T00:35:00+00:00", created = "2020-04-07T11:15:37+00:00",
closing = "2020-04-17T00:00:00+00:00"), country = list(list(
href = "https://api.reliefweb.int/v1/countries/149", name = "Mali",
location = list(lon = -1.25, lat = 17.35), id = 149L, shortname = "Mali",
iso3 = "mli")), title = "REGIONAL MANAGER West Africa")),
list(id = "3594129", score = 1L, fields = list(date = list(
changed = "2020-05-19T00:04:01+00:00", created = "2020-05-04T15:20:37+00:00",
closing = "2020-05-18T00:00:00+00:00"), title = "Support Relief Group Public Health Advisor (Multiple Positions)")))
【问题讨论】:
-
我认为没有人愿意将您的
str-rendering 转录为可用列表。请edit您的问题并粘贴dput(raw_jobs_list)的输出,谢谢。 -
谢谢@r2evans。 dput(raw_jobs_list) 的输出有点太重了,但是我现在已经在上面添加了初始代码,所以你可以轻松地运行它来尝试! :) ```
-
我建议使用(基本上)您最初在
str输出中提供的输出部分进行编辑。我包含了第二个元素,因为它与第一个元素有很大不同,人们应该知道它来查找缺失的字段。
标签: r list dataframe nested-lists read-data