This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(ggplot2)
You can also embed plots, for example:
outlines:
You can read a TXT file in R with the
read.table function. Importing TXT into R
rarely needs more arguments than specified. In the following subsections
we will explain two more (skip and
skipNul) if needed, but in case you want to know
all the arguments you can find them in the read.table
function documentation or calling
?read.table. This basic syntax affects to
almost all TXT data files.
Basic syntax
read.table(file, # TXT data file indicated as string or full path to the file
header = FALSE, # Whether to display the header (TRUE) or not (FALSE)
sep = "", # Separator of the columns of the file dec = ".") # Character used to separate decimals of the numbers in the file
Consider, for instance, that yo have a TXT file
called my_file.txt and you
have put it in your R working directory. You can read
it with the following code if you want to also display the header
(column names).
data <- read.table(file = "my_file.txt", header = TRUE)
head(data)
## x y
## 1 10 34.7081
## 2 12 34.5034
## 3 14 36.5656
## 4 16 38.3125
## 5 18 42.5441
## 6 20 43.7210
`In case you have the file in other directory than your working directory, you will need to specify the full path where the data file
data <- read.table(file = “C:\My_path\my_file.txt”, header = TRUE) data <- read.table(file = “C:/My_path/my_file.txt”, header = TRUE) # Equivalent
There also two functions (read.delim and
read.delim2) to deal with delimited files by
default. This functions have the following default arguments:
Syntax
read.delim(file = "my_file.txt" header = TRUE, sep = "\t", dec = ".")
read.delim2(file = "my_file.txt", header = TRUE, sep = "\t", dec = ",")
| Function | Header | Sep | Dec |
|---|---|---|---|
| read.table | FALSE | "" | "." |
| read.delim | TRUE | "\t" | "." |
| read.delim2 | TRUE | "\t" | "," |
Sometimes the TXT files you are reading contain some lines of
text before the dataset. To solve this issue you can use the
skip argument, that defaults to 0.
Consider, for instance, that there are 5 lines of text before your data,
so you can read the file the following way:
read.table(file = "my_file.txt", skip = 5)
## V1 V2
## 1 18 42.5441
## 2 20 43.7210
## 3 22 45.7065
## 4 24 46.4718
## 5 26 49.7821
## 6 28 51.1522
## 7 30 55.1607
## 8 32 54.7800
## 9 34 56.9059
## 10 36 59.5827
## 11 38 61.4808
## 12 40 65.4764
## 13 42 65.8270
## 14 44 66.5816
## 15 46 68.5623
## 16 48 72.0188
## 17 50 73.1321
If your TXT data contains NULL values,
you can set the skipnul argument to
TRUE to
read.table(file = “C:\My_path\my_file”, skipnul = TRUE)
In case you have a TXT file hosted in some website, you can open it without downloading it. You just need to pass the URL as string to the first argument.
url <- "http://courses.washington.edu/b517/Datasets/string.txt"
data <- read.table(url, header = TRUE)
head(data)
## x y
## 1 10 34.7081
## 2 12 34.5034
## 3 14 36.5656
## 4 16 38.3125
## 5 18 42.5441
## 6 20 43.7210
Now that you know how to read a TXT in R, it should be noticed that
you can directly download a TXT file in R to your
working directory with the download.file
function, passing the link as the first argument and the name you want
to put to the .txt file as the second.
getwd() # Path where the file will be downloaded
## [1] "C:/Users/immju/OneDrive/Documents"
url <- "http://courses.washington.edu/b517/Datasets/string.txt"
download.file(url, "my_file.txt")
If you don't want to download the file to the working directory, you can specify the path where you want the file to be downloaded.
download.file(url, "C:\\folder\\my_file.txt")
## Warning in download.file(url, "C:\\folder\\my_file.txt"): URL
## http://courses.washington.edu/b517/Datasets/string.txt: cannot open destfile
## 'C:\folder\my_file.txt', reason 'No such file or directory'
## Warning in download.file(url, "C:\\folder\\my_file.txt"): download had nonzero
## exit status
Note that the echo = FALSE parameter was added to
the code chunk to prevent printing of the R code that generated the
plot.