0

I'm attempting to run a loop to iterate through a number of adjacency lists saved as .csv files, convert them to edge lists, and network objects, and save each of these using the filename.

The problem is a) the code appears to cycle through the list of filenames but not produce any output b) it won't save the network objects using the filenames (it appears to overwrite each time).

Note that the code works fine for an individual file identified by "filename.csv" in replacement of the "f"s in the for-loop.

    l.files <- list.files(patt='.*csv$')
    i <- 0
    for (f in l.files) {

        lines=scan(f,what="character",sep="\n")
        lines=gsub(","," ",lines)
        lines=gsub("[ ]+$","",gsub("[ ]+"," ",lines))
        adjlist=strsplit(lines," ")
        col1=unlist(lapply(adjlist,function(x) rep(x[1],length(x)-1)))
        col2=unlist(lapply(adjlist,"[",-1))
        el=cbind(col1,col2)
        #If second column of el contains 0 then delete
        row_sub = apply(el, 1, function(row) all(row !=0 ))
        #This subset then saved as the new edgelist
        el <- el[row_sub,]
        #Save edgelist using the filename
        el[f] <- el
        summary(el)
        i=i+1
    }

Any help would be very much appreciated!

Tom Davidson
  • 737
  • 1
  • 8
  • 16
  • You never get `i` involved the way you want. I guess what you want is `for( i in 1:length( l.files ) )`. If that is the case, you don't need the `i <- 0` and `i <- i + 1` lines and have to replace f with i in `el[ i ] <- el`. However, without sample data, I cannot test, but maybe it gets you started – vaettchen Jul 01 '15 at 15:39
  • Thanks, actually ignore the indexing I'm not using that so its left in by mistake. – Tom Davidson Jul 01 '15 at 16:06
  • I guess a more simple example would be: for (f in l.files) { open file, save file as edge list using filename i.e. file1 <- el } such that each file now has an edge list saved in R with the corresponding file name – Tom Davidson Jul 01 '15 at 16:06
  • So job done after all? – vaettchen Jul 01 '15 at 18:21

1 Answers1

0

To open the files, you can use system, e.g.:

l.files <- system('ls *.csv', intern=T)
file.objs <- lapply(l.files, read.table)

Then you should be able to easily convert the items in file.objs to edgelists.

Frank
  • 66,179
  • 8
  • 96
  • 180
Chris Watson
  • 1,347
  • 1
  • 9
  • 24
  • Good point but not a real answer... -- admittedly to a question that was never asked clearly! – vaettchen Jul 01 '15 at 18:20
  • I've got it, thanks. Sorry it was not so clear, I guess I had a number of questions. I used the apply command in the end to save the objects as the loop was running using the index value. – Tom Davidson Jul 01 '15 at 18:58
  • So you want to, in a loop, open each file, convert that object to an edgelist, then save the edgelist to a file? I think in that case the best way would be to have each adjacency list file named something like 'graph1_alist.csv'. Then you can split the string, and save the edgelist with something like `write.csv(elist, file=paste0(basename, '_elist.csv')`. Where in this example `basename == graph1_`. – Chris Watson Jul 01 '15 at 19:17