0
votes

I want to import a triangular dataset (33 elements on first line, 32 on the second line, 31 on third line,...)

I tried:

Xij=read.table( file=file.choose(), header=FALSE)

which gives me the error: Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : line 2 did not have 9 elements

Can somebody please help me solving this?

Many thanks in advance!

1
use fill=TRUE argument in read.table - akrun

1 Answers

0
votes

You could do the following:

lines <- readLines(file.choose())
data <- strsplit(lines, ' ')

You will have the list of lines in 'data', and you can create a data frame according to your needs. E.g.:

n <- length(data)
m <- length(data[[n]])

for(i in 1:n) {
    data[[i]] <- as.numeric(data[[i]])
    length(data[[i]]) <- m
}

df <- data.frame(matrix(unlist(data), nrow=n, byrow=T))