使用 R 在文本文件中查找数据的直径
Find the Diameter of data in a text file using R
我有一个包含 20000 行和 3 列的文件 Here you can find a sample from the out1.txt file。我正在尝试将这些线导入 R 然后找到每 20 条线的直径(我的意思是将 20000 条线分成 1000 个部分,每个部分由 20 条线组成)所以首先我需要分别读取每 20 条线然后应用直径函数在 R.
之后我需要将每个部分读成图表然后找出直径。
包含 20000 行的文件的名称是 (out1.txt),我将用来在其上写入 100 个零件的直径的文件的名称是 outfile(因此 outfile 将包含100条线每条线代表out1.txt)
中20条线的直径
密码是:
setwd("Desktop")
outfile<- file('outfile', 'w')
library("igraph")
X<-read.table("out1.txt")
l=1;
t=1;
while (t<=20000){
for (i in t:t+19){
for (k in 1:3) {
T<- matrix(nrow=20, ncol=3);
T[l,k]<-X[i,k];
}
l=l+1;
}
t=t+20;
l=1;
gg<-graph.data.frame(T,directed=FALSE)
C<-diameter(gg,directed=FALSE,weights=NULL)
writeLines(C,con=outfile)
}
我遇到一个错误,但我无法理解它到底是什么意思,错误:
Error in writeLines(C, con = outfile) : invalid 'text' argument
非常感谢任何建议或帮助
我认为您可以稍微简化您的代码,因为您需要 20 个块。此外,我认为您需要附加结果,否则,您将在每个循环中覆盖您的结果。试试这个:
DF <- data.frame(C1=sample(1:1000,200), C2=sample(1:1000,200))
outfile<- file('outfile.txt')
for(i in seq(1,200, 20)){
for(j in seq(20,200, 20)){
g <- as.character(diameter(graph.data.frame(DF[i:j, ], directed = F),directed=FALSE,weights=NULL))
write.table(g,"outfile.txt", append = T, col.names = F, row.names = F)
}
}
close(outfile)
我有一个包含 20000 行和 3 列的文件 Here you can find a sample from the out1.txt file。我正在尝试将这些线导入 R 然后找到每 20 条线的直径(我的意思是将 20000 条线分成 1000 个部分,每个部分由 20 条线组成)所以首先我需要分别读取每 20 条线然后应用直径函数在 R.
之后我需要将每个部分读成图表然后找出直径。
包含 20000 行的文件的名称是 (out1.txt),我将用来在其上写入 100 个零件的直径的文件的名称是 outfile(因此 outfile 将包含100条线每条线代表out1.txt)
中20条线的直径密码是:
setwd("Desktop")
outfile<- file('outfile', 'w')
library("igraph")
X<-read.table("out1.txt")
l=1;
t=1;
while (t<=20000){
for (i in t:t+19){
for (k in 1:3) {
T<- matrix(nrow=20, ncol=3);
T[l,k]<-X[i,k];
}
l=l+1;
}
t=t+20;
l=1;
gg<-graph.data.frame(T,directed=FALSE)
C<-diameter(gg,directed=FALSE,weights=NULL)
writeLines(C,con=outfile)
}
我遇到一个错误,但我无法理解它到底是什么意思,错误:
Error in writeLines(C, con = outfile) : invalid 'text' argument
非常感谢任何建议或帮助
我认为您可以稍微简化您的代码,因为您需要 20 个块。此外,我认为您需要附加结果,否则,您将在每个循环中覆盖您的结果。试试这个:
DF <- data.frame(C1=sample(1:1000,200), C2=sample(1:1000,200))
outfile<- file('outfile.txt')
for(i in seq(1,200, 20)){
for(j in seq(20,200, 20)){
g <- as.character(diameter(graph.data.frame(DF[i:j, ], directed = F),directed=FALSE,weights=NULL))
write.table(g,"outfile.txt", append = T, col.names = F, row.names = F)
}
}
close(outfile)