Open In App

Read Only Header of File in R

Last Updated : 30 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see the header of the file in R Programming language.

Method 1: Using read.table method

The read.table() method in R is used to access the contents stored in the organized structure of the CSV file.

Syntax: read.table(path, head = TRUE, nrows, sep = “;”)

Arguments:

  • path: The complete directory path where the file is stored
  • head: a logical value, indicator of whether the file contains the names of the variables as its first line or not.
  • nrows: the maximum number of rows to read into the working directory

Code:

R




# creating a data frame
data_frame1 <- data.frame(col1 = c(6:8),
                          col2 = letters[1:3],
                          col3 = c(1,4,NA))
 
# specifying data path
path <- "/Users/mallikagupta/Desktop/rcontent.csv"
 
# writing data to csv
write.csv2(data_frame1,                  
           path,
           row.names = FALSE)
# reading
read.table(path,            
           head = TRUE,
           nrows = 1,
           sep = ";")[- 1, ]


Output

[1] col1 col2 col3
<0 rows> (or 0-length row.names)

Method 2: Using colnames method

The read.csv2() method in R is used to read the CSV file from the specified path argument into a tabular structure.

Syntax: read.csv2(path)

Arguments :

path: The complete directory path where the file is stored

colnames() method in base R is used to gather the column names assigned to the tabular structure of the CSV file. It takes as argument the tabular structure read from the file.

Syntax: colnames(df)

Arguments :

df: The data frame to read the header of

Code:

R




# creating a data frame
data_frame1 <- data.frame(col1 = c(6:8),
                          col2 = letters[1:3],
                          col3 = c(1,4,NA))
 
# specifying data path
path <- "/Users/mallikagupta/Desktop/rcontent.csv"
 
# writing data to csv
write.csv2(data_frame1,                  
           path,
           row.names = FALSE)
 
# reading header
colnames(read.csv2(path))


Output

[1] "col1" "col2" "col3"


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads