我有一个数据,显示了几个用户执行的一系列操作(列<code>actions</code>)(列<code>Id</code>)。数据帧的顺序很重要——它是执行操作的顺序。对于每个id,执行的第一个操作是<code>start</code>。可以连续执行相同的操作(例如,序列<code>start-
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可视化中使用(就像这样)。我希望看到一个计数器,显示每个孩子为他们各自的父母出现的次数(甚至可能是父母总出现次数的百分比)——但我希望我自己能做到这一点。
下面的预期输出-这是通用的,而不是我上面生成的数据,实际数据将有相当多的嵌套值(count
和百分比
在此时是可选的):
{
"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"
}
]
}
我知道我应该发布一些我尝试过的东西,但是我真的没有任何值得展示的东西。
我刚开始写一些R-
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))