Closed krpaulson closed 4 years ago
I've tried a few modifications and it seems that in general it is omitting the lowest level.
You only need the parent
and child
columns like in iran_mapping
. There should be a better assertion for that
dt <- data.table::data.table(
location_1 = c("east", "east", "east"),
location_2 = c("east", "west", "all")
)
mapping <- data.table::data.table(
col = rep("location", 4),
parent = rep("all", 4),
child = c("east", "west", "north", "south")
)
tree <- data.tree::FromDataFrameNetwork(mapping)
dt[, distance_location :=
data.tree::Distance(tree[[location_1]], tree[[location_2]])]
Thanks! I might just use the FromDataFrameNetwork
function directly actually. Do you have an idea of how to get this to work? Trying to use the tree within a data.table to assign the distance between locations in two columns. But getting an error on this test example that I'm not sure how to solve.
I think your mapping is still messed up, delete the col
column.
Something like data.tree::Distance(data.tree::FindNode(node = tree, name = "east"), data.tree::FindNode(node = tree, name = "north"))
Yeah, I got there but this function is not vectorized. We can close this issue here and I'll move over to lifetableMethods
.
The above only gives me the top level in my tree ("all"), when I expect to get the next level down too, with the regions.
I'd add that the description for
exists
could be clearer. Perhaps "unique values in the hierarchy"?