给出以下结构:
$Days
我想对其进行“整理”,以将所有列表内容作为附加列并进行虚拟化:
所需结果:
$Results
每行等。
请告知如何执行此操作,我正在尝试使用df <- structure(list(checkPointAdditionalData_ignorePermissions = list(
list("READ_EXTERNAL_STORAGE", "READ_CONTACTS", "READ_PHONE_STATE",
"WRITE_EXTERNAL_STORAGE", "ACCESS_FINE_LOCATION", "ACCESS_COARSE_LOCATION",
"READ_CALL_LOG"), list("READ_EXTERNAL_STORAGE", "READ_CONTACTS",
"READ_PHONE_STATE", "WRITE_EXTERNAL_STORAGE", "ACCESS_FINE_LOCATION",
"ACCESS_COARSE_LOCATION", "READ_CALL_LOG"), list("READ_EXTERNAL_STORAGE",
"READ_CONTACTS", "READ_PHONE_STATE", "WRITE_EXTERNAL_STORAGE",
"READ_CALL_LOG"), list("READ_EXTERNAL_STORAGE", "READ_CONTACTS",
"READ_PHONE_STATE", "WRITE_EXTERNAL_STORAGE", "READ_CALL_LOG"),
list("READ_EXTERNAL_STORAGE", "READ_CONTACTS", "READ_PHONE_STATE",
"WRITE_EXTERNAL_STORAGE", "READ_CALL_LOG"), list("READ_EXTERNAL_STORAGE",
"READ_CONTACTS", "READ_PHONE_STATE", "WRITE_EXTERNAL_STORAGE",
"READ_CALL_LOG"), list("READ_EXTERNAL_STORAGE", "READ_CONTACTS",
"READ_PHONE_STATE", "WRITE_EXTERNAL_STORAGE", "READ_CALL_LOG"),
list("READ_EXTERNAL_STORAGE", "READ_CONTACTS", "READ_PHONE_STATE",
"WRITE_EXTERNAL_STORAGE", "READ_CALL_LOG"), list("READ_EXTERNAL_STORAGE",
"READ_CONTACTS", "READ_PHONE_STATE", "WRITE_EXTERNAL_STORAGE",
"READ_CALL_LOG"), list("READ_EXTERNAL_STORAGE", "READ_CONTACTS",
"READ_PHONE_STATE", "WRITE_EXTERNAL_STORAGE", "READ_CALL_LOG")),
L_ignore_perm = c(7L, 7L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -10L))
函数。
我完全理解,流应该是列表中所有可能成员的 READ_EXTERNAL_STORAGE READ_CONTACTS READ_PHONE_STATE
1 1 1
1 1 1
1 1 1
WRITE_EXTERNAL_STORAGE ACCESS_FINE_LOCATION ACCESS_COARSE_LOCATION
1 1 1
1 1 1
1 0 0
READ_CALL_LOG
1
1
1
,将其添加为列名,对每一列应用tidyr
,以将union
作为值。
没有一种功能可以简单有效地实现吗?
我已经做了以下事情:
ifelse
并尝试使用1/0
软件包。
答案 0 :(得分:2)
您可以尝试:
library(tidyverse)
df$checkPointAdditionalData_ignorePermissions %>%
map_df(data.frame) %>%
set_names(map(., levels)) %>%
mutate_all(function(x) if_else(is.na(x), 0, 1))
READ_EXTERNAL_STORAGE READ_CONTACTS READ_PHONE_STATE WRITE_EXTERNAL_STORAGE ACCESS_FINE_LOCATION ACCESS_COARSE_LOCATION READ_CALL_LOG
1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1
3 1 1 1 1 0 0 1
4 1 1 1 1 0 0 1
5 1 1 1 1 0 0 1
6 1 1 1 1 0 0 1
7 1 1 1 1 0 0 1
8 1 1 1 1 0 0 1
9 1 1 1 1 0 0 1
10 1 1 1 1 0 0 1