Hello everone, I am a computer science researcher in the UK - and have a quick question regarding importing data into R and more specifically latentnet. I am trying to import data such as a text file containing: (This is based on the Sampson data set) ----------- Network attributes: vertices = 3 directed = TRUE hyper = FALSE loops = FALSE multiple = FALSE total edges= 3 Vertex attribute names: group vertex.names edgelist matrix: [,1] [,2] [1,] 3 1 [2,] 2 1 [3, ] 3 2 ------------ What command should I use to import this data? I have attempted many such as "read.delim" and "read.table" - but either an error message is returned, or the strings in the data (such as edges=3) appear to become objects that are analysed by the latentnet functions such as 'ergmm'. I look forward to your replys, Many thanks Ben
It all depends. If your looks like your sample, then you will probably want to read the data with 'readLine' and bring in the entire line without trying to parse it. You would then go through the data with 'grep' to find the lines of interest and then you can use 'strsplit' or regular expressions to parse the line to get the data. There is probably no "automatic" way of doing it. You might also consider PERL to preprocess the data and put it into a format that is more acceptable to R. But most of it can probably be done within R if the structure of your input is reasonable. On 3/26/08, Ben Morley <me at benmorley.com> wrote:> > Hello everone, > > I am a computer science researcher in the UK - and have a quick question regarding importing data into R and more specifically latentnet. > > I am trying to import data such as a text file containing: (This is based on the Sampson data set) > > ----------- > > Network attributes: > vertices = 3 > directed = TRUE > hyper = FALSE > loops = FALSE > multiple = FALSE > total edges= 3 > > Vertex attribute names: > group vertex.names > > edgelist matrix: > [,1] [,2] > [1,] 3 1 > [2,] 2 1 > [3, ] 3 2 > > ------------ > > What command should I use to import this data? I have attempted many such as "read.delim" and "read.table" - but either an error message is returned, or the strings in the data (such as edges=3) appear to become objects that are analysed by the latentnet functions such as 'ergmm'. > > I look forward to your replys, > > Many thanks > > Ben > > > > > > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Try this. If the format varies you may need to modify it from below. Replace textConnection(rawLines) with the name of your file. This extracts lines with a = and rereads them using read.table with = as the separator. It also gets lines with a number that has spaces on both sides, deletes everything to the ] and rereads them using read.table. rawLines <- " Network attributes: vertices = 3 directed = TRUE hyper = FALSE loops = FALSE multiple = FALSE total edges= 3 Vertex attribute names: group vertex.names edgelist matrix: [,1] [,2] [1,] 3 1 [2,] 2 1 [3, ] 3 2 " Lines <- readLines(textConnection(rawLines)) Lines0 <- grep("=", Lines, value = TRUE) attributes <- read.table(textConnection(Lines0), sep = "=", as.is = TRUE) Lines1 <- sub(".*\\]", "", grep(" [0-9]+ ", Lines, value = TRUE)) edgelist <- read.table(textConnection(Lines1)) On Wed, Mar 26, 2008 at 7:15 AM, Ben Morley <me at benmorley.com> wrote:> > Hello everone, > > I am a computer science researcher in the UK - and have a quick question regarding importing data into R and more specifically latentnet. > > I am trying to import data such as a text file containing: (This is based on the Sampson data set) > > ----------- > > Network attributes: > vertices = 3 > directed = TRUE > hyper = FALSE > loops = FALSE > multiple = FALSE > total edges= 3 > > Vertex attribute names: > group vertex.names > > edgelist matrix: > [,1] [,2] > [1,] 3 1 > [2,] 2 1 > [3, ] 3 2 > > ------------ > > What command should I use to import this data? I have attempted many such as "read.delim" and "read.table" - but either an error message is returned, or the strings in the data (such as edges=3) appear to become objects that are analysed by the latentnet functions such as 'ergmm'. > > I look forward to your replys, > > Many thanks > > Ben > > > > > > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >