Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

How I can find the connect values by 2 columns in R

I have a dataset with 2 columns. I need to find which words connect dt$source == "learn4 in R.

The output is a vector c("learn2", "learn1", "learn")

First we have "learn4" connected to "learn2",then "learn2" connected to "learn1", then "learn1 connected to "learn"

I couldn't think any way to approach the problem. Any suggestion?

dt <- data.frame(source = c("learn","learn1", "disc","learn2","learn3","disc1","lb","learn4"),
                 new = c("learn","learn","disc","learn1","learn1","disc","lb","learn2"))
 dt
  source   new
1  learn  learn
2 learn1  learn
3   disc   disc
4 learn2 learn1
5 learn3 learn1
6  disc1   disc
7     lb     lb
8 learn4 learn2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Another igraph apprach would be

gg <- igraph::graph_from_data_frame(dt)

longest_path <- function(graph, to) { 
  x <- all_simple_paths(gg, to)
  names(x[[which.max(lengths(x))]])
}

longest_path(gg, "learn4")
# [1] "learn4" "learn2" "learn1" "learn" 

This looks how far away you can get from the input node.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...