我正在尝试取消列出列表列,其中每行仅包含一个网址。
我创建了一个带有文件元数据的数据帧,该文件的目录中有一些文件。这是我的代码:
df <- files_small %>%
keep(has_xattrs) %>%
set_names(basename(.)) %>%
map_df(read_xattrs, .id = "file") %>%
filter(name == "com.apple.metadata:kMDItemWhereFroms") %>%
mutate(url = map(files_small,
~ read_bplist(
get_xattr_raw(path = .x,
name = "com.apple.metadata:kMDItemWhereFroms")
)
)) %>%
select(file, url)
这将导致以下dput
:
df <- structure(list(file = c("'s-Gravenhage_coalitieakkoord.pdf",
"Aa en Hunze_coalitieakkoord.pdf"), url = list(structure(list(
plist = structure(list(array = structure(list(string = list(
"https://denhaag.raadsinformatie.nl/document/6514256/2/RIS299794%20Coalitieakkoord%202018%202022"),
string = list()), .Names = c("string", "string"))), .Names = "array", version = "1.0")), .Names = "plist"),
structure(list(plist = structure(list(array = structure(list(
string = list("https://aaenhunze.vvd.nl/uploaded/aaenhunze.vvd.nl/files/5ad8cd44682d8/coalitieakkoord-2018-2022-_definitief_16-april.pdf"),
string = list()), .Names = c("string", "string"))), .Names = "array", version = "1.0")), .Names = "plist"))), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -2L), .Names = c("file",
"url"))
> unnest(df, url)
# A tibble: 2 x 2
file url
<chr> <list>
1 's-Gravenhage_coalitieakkoord.pdf <list [1]>
2 Aa en Hunze_coalitieakkoord.pdf <list [1]>
我想取消列出列表列,但是unnest(df, url)
似乎没有完成其工作。我在这里做什么错了?
答案 0 :(得分:1)
您有几个嵌套列表,请尝试:
df %>% rowwise %>% mutate(x=unlist(url))
# # A tibble: 2 x 3
# file url x
# <chr> <list> <chr>
#1 's-Gravenhage_coalitieakkoord.pdf <list [1]> https://denhaag.raadsinformatie.nl/document/6514256/2/RIS299794%20Coalitieakkoord%202018%202022
#2 Aa en Hunze_coalitieakkoord.pdf <list [1]> https://aaenhunze.vvd.nl/uploaded/aaenhunze.vvd.nl/files/5ad8cd44682d8/coalitieakkoord-2018-2022-_definitief_16-april.pdf