R Read Lines of a Text File

readLines, n.readLines & readline in R (6 Example Codes)

In this tutorial, I'chiliad going to prove you how to read text by line with iii different R functions:

  1. readLines (Examples one-4)
  2. n.readLines (Instance five)
  3. readline (Example 6)

Let's outset with the basic R syntax of these three functions and some definitions:

Basic R Syntax:

readLines(                "path/filename.txt"                )                n.                readLines                (                "path/filename.txt"                , n                =                5, skip                =                2                )                readline(                "question"                )              

readLines("path/filename.txt") due north.readLines("path/filename.txt" , n = v, skip = 2) readline("question")

The readLines part reads text lines from an input file.

The n.readLines function of the reader packet provides additional functionalities for reading lines, such as skipping ahead in a file or ignoring comments and headers.

The readline function interactively reads a line from the concluding.

In order to become a bit more concrete, let's movement on to the examples…

Example 1: Read Lines of txt File via readLines R Part

When you lot have to exercise text mining / text analysis of larger texts, you will typically be provided with relatively unstructured .txt files.

The readLines part is perfect for such text files, since it reads the text line by line and creates character objects for each of the lines.

For the offset example, I'thou going to create a elementary txt file that we can use for the application of readLines. In case you desire to reproduce the instance, simply copy and paste the post-obit code.

So, let's first store the directory, where we want to store and load our instance data…

                # Store currently used directory                path                <-                getwd(                )              

# Store currently used directory path <- getwd()

…and and so let's create a txt file in this directory:

                # Write example text to currently used directory                write.                table                (x                =                impress(                "this is the first line\nthis is the 2nd line\northwardthis is the third line"                ),             file                =                paste(path,                "/my_txt.txt", sep                =                ""                ),             row.                names                =                False, col.                names                =                FALSE, quote                =                Imitation                )              

# Write example text to currently used directory write.table(ten = print("this is the first line\nthis is the second line\nthis is the tertiary line"), file = paste(path, "/my_txt.txt", sep = ""), row.names = FALSE, col.names = Fake, quote = Faux)

If you run this code on your figurer, there should be a new txt file in the binder that is currently used by R (check the folder location via getwd()). The txt file looks every bit follows:

txt File with Several Text Lines

Figure 1: Text File for the Application of readLines().

At present, we can utilize the R readLines command to this text file:

                # Apply readLines function to txt file                my_txt                <-                readLines(paste(path,                "/my_txt.txt", sep                =                ""                )                )                my_txt                # "this is the start line"  "this is the second line" "this is the third line"              

# Employ readLines role to txt file my_txt <- readLines(paste(path, "/my_txt.txt", sep = "")) my_txt # "this is the first line" "this is the second line" "this is the tertiary line"

The output of the role is a vector that contains three character strings, i.eastward. this is the start line, this is the second line, and this is the third line.

As y'all tin can see, nosotros read the whole txt file into R. Piece of cake – But what if nosotros want to read only certain lines from our text file?

Instance ii: Read Kickoff n Lines Only

Quite frequently you will be interested in the start due north lines of your input file. Fortunately the readLines R role provides an n-option, which lets you specify the number of lines to read.

We can simply adjust our code as follows…

                # Apply readLines function to first two lines                my_txt_ex2                <-                readLines(paste(path,                "/my_txt.txt", sep                =                ""                ),                         n                =                2                )                my_txt_ex2                # "this is the outset line"  "this is the second line"              

# Utilize readLines part to first two lines my_txt_ex2 <- readLines(paste(path, "/my_txt.txt", sep = ""), n = ii) my_txt_ex2 # "this is the first line" "this is the 2nd line"

…in lodge to read merely the showtime two lines of our example file.

Looks proficient. Even so, so far we have only used .txt files. What about other file-types?

Example 3: readLines from CSV File into R

In this case, I'one thousand going to use the readLines R part to read a data frame that is stored in a .csv file.

Permit's offset create an example file in our currently used directory:

                # Write example csv to currently used directory                write.                csv                (iris,           paste(path,                "/iris.csv", sep                =                ""                ),           quote                =                Faux                )              

# Write example csv to currently used directory write.csv(iris, paste(path, "/iris.csv", sep = ""), quote = Faux)

If you accept a wait at the currently used binder on your computer, you will detect the Iris data fix. The offset few rows of the information expect equally follows:

nrow function in R - Iris Example Data Frame

Table 1: Start half dozen Rows of Iris Data Set.

Nosotros can apply the readLines office to this csv equally nosotros did before:

                # Apply readLines part to csv file                iris_data                <-                readLines(paste(path,                "/iris.csv", sep                =                ""                ),                        north                =                iv                )                iris_data                # [1] ",Sepal.Length,Sepal.Width,Petal.Length,Petal.Width,Species" "one,5.ane,3.5,1.four,0.2,setosa"                # [3] "2,iv.ix,three,1.4,0.2,setosa"                                     "3,iv.7,3.2,i.3,0.2,setosa"              

# Apply readLines function to csv file iris_data <- readLines(paste(path, "/iris.csv", sep = ""), northward = 4) iris_data # [1] ",Sepal.Length,Sepal.Width,Petal.Length,Petal.Width,Species" "1,5.one,three.v,i.4,0.two,setosa" # [iii] "two,four.ix,three,i.iv,0.2,setosa" "3,4.7,3.two,i.3,0.2,setosa"

readLines returns a character object for each row of the data frame, whereby columns are separated by commas.

Case 4: readLines from xlsx Excel File into R?!

In the previous Example, I have shown you how to read csv Excel files with the readLines function. Now you might inquire: is it also possible to utilize readLines for xlsx Excel files?

Answer: As far every bit I know, it is not.

Fortunately, there is an like shooting fish in a barrel work-around in case you want to apply readLines to xlsx files – Just convert your xlsx file to csv!

I'thou going to prove you lot how:

First, nosotros need to install and load the xlsx R package:

                # Install and load xlsx bundle                install.                packages                (                "xlsx"                )                library(                "xlsx"                )              

# Install and load xlsx package install.packages("xlsx") library("xlsx")

Then, we can utilise the write.xlsx function to create an xlsx file for our example (we are using the iris information set once more):

                # Write example xlsx to currently used directory                write.                xlsx                (iris,            paste(path,                "/iris_xlsx.xlsx", sep                =                ""                ),            row.                names                =                FALSE                )              

# Write example xlsx to currently used directory write.xlsx(iris, paste(path, "/iris_xlsx.xlsx", sep = ""), row.names = FALSE)

At this bespeak you should take an xlsx file with the proper name iris_xlsx in your working directory.

Now, we can apply the following R code in order to catechumen the xlsx file to csv:

                # Convert xlsx to csv                iris_xlsx                <-                read.                xlsx2                (paste(path,                "/iris_xlsx.xlsx", sep                =                ""                ),                         sheetIndex                =                1                )                write.                csv2                (iris_xlsx,            paste(path,                "/iris_converted.csv", sep                =                ""                )                )              

# Convert xlsx to csv iris_xlsx <- read.xlsx2(paste(path, "/iris_xlsx.xlsx", sep = ""), sheetIndex = ane) write.csv2(iris_xlsx, paste(path, "/iris_converted.csv", sep = ""))

At this signal you should take a csv file with the proper noun iris_converted in your working directory.

After the conversion, you tin can simply use readLines, equally I have shown you in Instance 3.

Easy breezy!

Example v: Skip First Lines via n.readLines [reader Bundle]

Some other quite common scenario is that you are interested of some lines within your text, i.e. you lot desire to skip the first n lines and possibly also the final n lines.

Fortunately, the R bundle reader provides such options. Let'southward first install and load the package:

                # Install and load reader R package                install.                packages                (                "reader"                )                library(                "reader"                )              

# Install and load reader R bundle install.packages("reader") library("reader")

We could also apply the n.readLines function to produce the same output equally we did with readLines of base R in Example ane:

                # Apply n.readLines role                due north.                readLines                (paste(path,                "/my_txt.txt", sep                =                ""                ),             header                =                Fake,             n                =                three                )                # "this is the first line"  "this is the second line" "this is the third line"              

# Employ n.readLines role n.readLines(paste(path, "/my_txt.txt", sep = ""), header = Faux, n = 3) # "this is the first line" "this is the second line" "this is the third line"

However, the n.readLines function provides an additional skip-option:

                # Use north.readLines function with skip option                north.                readLines                (paste(path,                "/my_txt.txt", sep                =                ""                ),             header                =                Faux,             n                =                ii,             skip                =                ane                )                # "this is the 2d line" "this is the 3rd line"              

# Use northward.readLines part with skip choice n.readLines(paste(path, "/my_txt.txt", sep = ""), header = FALSE, north = two, skip = 1) # "this is the second line" "this is the third line"

Nosotros take used north = ii in order to print ii lines and we have specified skip = ane in order to skip the offset line.

Example 6 (Video): readLines vs. readline – What's the difference?

Often confusing: Base R provides a part that is chosen readLines (with upper case Fifty and an s at the end) and a function that is called readline (all in lower case and no s at the end).

Even though both functions are related to each other, they are used for different situations. While readLines is used to read the lines of an input file, readline is used to read the input of the R user interactively (typically by asking questions to the user in the RStudio console).

It is hard to explain an interactive function in a written tutorial. However, fortunately the YouTube channel Docworld University has created a simple video on the usage of readline in R.

Accept fun with the video and let me know in the comments, in example y'all have any questions.

Farther Reading

  • The R Programming Language

lykeyonge1956.blogspot.com

Source: https://statisticsglobe.com/r-readlines-example

0 Response to "R Read Lines of a Text File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel