Read.table for reading in incomplete data in R

I have a large table to read in R, and the file is in format .txt. In R, I use a function read.table, but there is an error reading it. The following error message appears:

Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  line 28 did not have 23 elements

It seems that (counting from the 1st row without regard to the header, as I indicated skip=), the data in line 28 have missing elements. I am looking for a way to automatically fix this problem by filtering this line. At the moment I can’t even read in the file, so I can’t manipulate in R ... Any suggestions are much appreciated :)

+4
source share
2 answers

: call read.table fill=TRUE , ( count.fields).

:

# 1. Data generation, and saving in 'tempfile'
cat("1 John", "2 Paul", "7 Pierre", '9', file = "tempfile", sep = "\n")

# 2. read the data:
data = read.table('tempfile',fill=T)

# 3. exclude incomplete data
c.fields = count.fields('tempfile')
data = data[ - (which(c.fields) != max(c.fields)),]

(, )

+4

, - (#).

, comment.char comment.char = ""

read.table("file.txt", comment.char = "")
+2

Source: https://habr.com/ru/post/1531440/


All Articles