R: Count the Number of Fields per Line

count.fields {utils} | R Documentation

## Count the Number of Fields per Line

### Description

`count.fields` counts the number of fields, as separated by `sep`, in each of the lines of `file` read.

### Usage

    count.fields(file, sep = "", quote = "\"'", skip = 0,
                 blank.lines.skip = TRUE, comment.char = "#")

### Arguments

`file` | a character string naming an ASCII data file, or a `connection`, which will be opened if necessary, and if so closed at the end of the function call.
`sep` | the field separator character. Values on each line of the file are separated by this character. By default, arbitrary amounts of whitespace can separate fields.
`quote` | the set of quoting characters
`skip` | the number of lines of the data file to skip before beginning to read data.
`blank.lines.skip` | logical: if `TRUE` blank lines in the input are ignored.
`comment.char` | character: a character vector of length one containing a single character or an empty string.

### Details

This used to be used by `read.table` and can still be useful in discovering problems in reading a file by that function.

For the handling of comments, see `scan`.

Consistent with `scan`, `count.fields` allows quoted strings to contain newline characters. In such a case the starting line will have the field count recorded as `NA`, and the ending line will include the count of all fields from the beginning of the record.

### Value

A vector with the numbers of fields found.

### See Also

`read.table`

### Examples

    fil <- tempfile()
    cat("NAME", "1:John", "2:Paul", file = fil, sep = "\n")
    count.fields(fil, sep = ":")
    unlink(fil)

[Package utils version 3.5.1 Index]