R Func Read Table
# R read.table() Function - Reading Table Files
[ R Language Examples](https://example.com/r/r-examples.html)
R read.table() function is the underlying generic version of read.csv(), used to read delimiter-separated text files.
read.table() can handle various delimiter-separated file formats, such as TSV (tab-separated), fixed-width, etc.
The syntax of read.table() function is as follows:
read.table(file, header = FALSE, sep = "", dec = ".", comment.char = "#", na.strings = "NA")
**Parameter Description:**
* **file** File path.
* **header** Whether the first row is used as column names.
* **sep** Delimiter, empty string means whitespace separator.
* **comment.char** Comment character, lines starting with this character are ignored.
* **na.strings** Strings to be treated as NA values.
## Example
# Write tab-separated file
write.table(mtcars[1:5, 1:3], "temp_mtcars.tsv",
sep ="\t", row.names= FALSE)
# View original content
print("TSV file content:")
cat(readLines("temp_mtcars.tsv"), sep ="
")
# Read tab-separated file
data<-read.table("temp_mtcars.tsv", header = TRUE, sep ="\t")
print("
Data read back:")
print(data)
Executing the above code outputs:
"TSV file content:""mpg" "cyl" "disp""21" "6" "160""21" "6" "160""22.8" "4" "108""21.4" "6" "258""18.7" "8" "360" "Data read back:" mpg cyl disp 1 21.0 6 1602 21.0 6 1603 22.8 4 1084 21.4 6 2585 18.7 8 360
[ R Language Examples](https://example.com/r/r-examples.html)
YouTip