我的数据显示了多个用户(列Actions
)执行的一系列操作(列Id
)。数据框的顺序很重要 - 它是执行操作的顺序。对于每个id,执行的第一个操作是start
。可以执行相同的相同操作(例如,序列start -> D -> D -> D
有效)。这是一些生成数据的代码:
set.seed(10)
i <- 0
all_id <- NULL
all_vals <- NULL
while (i < 5) {
i <- i + 1
print(i)
size <- sample(3:5, size = 1)
tmp_id <- rep(i, times = size + 1)
tmp_vals <- c("start",sample(LETTERS, size = size) )
all_id <- c(all_id, tmp_id)
all_vals <- c(all_vals, tmp_vals)
}
df <- data.frame(Id = all_id,
Action = all_vals)
目标 - 在嵌套在多个级别上的JSON中转换此数据,这些级别将在D3.js可视化中使用(如this)。我希望看到一个计数器,显示每个孩子为他们各自的父母出现的次数(甚至可能是父母总出现次数的百分比) - 但我希望自己可以做到这一点。
下面的预期输出 - 这是通用的,而不是我上面生成的数据,并且实际数据将具有相当多的嵌套值(此时count
和percentage
是可选的) :
{
"action": "start",
"parent": "null",
"count": "10",
"percentage": "100",
"children": [
{
"action": "H",
"parent": "start",
"count": "6",
"percentage": "60",
"children": [
{
"action": "D",
"parent": "H",
"count": "5",
"percentage": "83.3"
},
{
"action": "B",
"parent": "H",
"count": "3",
"percentage": "50"
}
]
},
{
"action": "R",
"parent": "start",
"count": "4",
"percentage": "40"
}
]
}
我知道我应该发布一些我尝试过的东西,但我真的没有任何东西值得展示。
答案 0 :(得分:1)
我刚开始写一些R - &gt;在https://github.com/timelyportfolio/d3r中的d3.js转换器应该在这些类型的情况下运行良好。我今天晚些时候会用你的数据做一个例子。
https://github.com/timelyportfolio/sunburstR中的内部层次结构构建器也可以在这里运行良好。
当我探索这两条路径时,我会添加答案。
set.seed(10)
i <- 0
all_id <- NULL
all_vals <- NULL
while (i < 5) {
i <- i + 1
print(i)
size <- sample(3:5, size = 1)
tmp_id <- rep(i, times = size + 1)
tmp_vals <- c("start",sample(LETTERS, size = size) )
all_id <- c(all_id, tmp_id)
all_vals <- c(all_vals, tmp_vals)
}
df <- data.frame(Id = all_id,
Action = all_vals)
# not sure I completely understand what this is
# supposed to become but here is a first try
# find position of start
start_pos <- which(df$Action=="start")
# get the sequences
# surely there is a better way but do this for now
sequences <- paste(
start_pos+1,
c(start_pos[-1],nrow(df))-1,
sep=":"
)
paths <- lapply(
sequences,
function(x){
data.frame(
t(as.character(df[eval(parse(text=x)),]$Action)),
stringsAsFactors=FALSE
)
}
)
paths_df <- dplyr::bind_rows(paths)
# use d3r
# devtools::install_github("timelyportfolio/d3r")
library(d3r)
d3_nest(paths_df) # if want list, then json=FALSE
# visualize with listviewer
# devtools::install_github("timelyportfolio/listviewer")
listviewer::jsonedit(d3_nest(paths_df))