Skip to content Skip to sidebar Skip to footer

Skip Reading Number of Lines at the End in R

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

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

  1. readLines (Examples 1-4)
  2. n.readLines (Example v)
  3. readline (Example 6)

Allow's start with the bones R syntax of these iii functions and some definitions:

Basic R Syntax:

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

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

The readLines function reads text lines from an input file.

The due north.readLines office of the reader packet provides additional functionalities for reading lines, such equally skipping ahead in a file or ignoring comments and headers.

The readline function interactively reads a line from the terminal.

In gild to become a fleck more physical, let's move on to the examples…

Case 1: Read Lines of txt File via readLines R Function

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

The readLines office 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 first instance, I'g going to create a elementary txt file that nosotros can employ for the application of readLines. In case you desire to reproduce the case, simply copy and paste the post-obit lawmaking.

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

                # Store currently used directory                path                <-                getwd(                )              

# Shop currently used directory path <- getwd()

…and and then allow'southward create a txt file in this directory:

                # Write case text to currently used directory                write.                table                (x                =                print(                "this is the kickoff line\nthis is the 2nd line\nthis is the third line"                ),             file                =                paste(path,                "/my_txt.txt", sep                =                ""                ),             row.                names                =                Fake, col.                names                =                Faux, quote                =                FALSE                )              

# Write case text to currently used directory write.table(x = print("this is the offset line\nthis is the 2nd line\nthis is the third line"), file = paste(path, "/my_txt.txt", sep = ""), row.names = FALSE, col.names = Faux, quote = Simulated)

If y'all run this code on your computer, there should be a new txt file in the folder that is currently used by R (cheque the folder location via getwd()). The txt file looks as follows:

txt File with Several Text Lines

Effigy i: Text File for the Application of readLines().

At present, we can apply 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 first line"  "this is the second line" "this is the 3rd line"              

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

The output of the part is a vector that contains iii grapheme strings, i.e. this is the first line, this is the second line, and this is the tertiary line.

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

Example 2: Read First northward Lines Only

Quite ofttimes yous volition be interested in the first north lines of your input file. Fortunately the readLines R part provides an north-option, which lets you specify the number of lines to read.

Nosotros can simply accommodate our code equally 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 first line"  "this is the second line"              

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

…in club to read only the first two lines of our example file.

Looks good. However, so far we accept only used .txt files. What nearly other file-types?

Example 3: readLines from CSV File into R

In this example, I'k going to utilise the readLines R office to read a data frame that is stored in a .csv file.

Let's outset create an example file in our currently used directory:

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

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

If y'all take a look at the currently used folder on your computer, you will find the Iris data set. The starting time few rows of the data expect as follows:

nrow function in R - Iris Example Data Frame

Tabular array i: Starting time 6 Rows of Iris Data Set.

We can apply the readLines function to this csv every bit we did before:

                # Use readLines function 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" "1,v.one,3.v,1.iv,0.ii,setosa"                # [3] "2,4.nine,3,1.4,0.ii,setosa"                                     "iii,4.7,three.2,i.3,0.ii,setosa"              

# Use readLines function to csv file iris_data <- readLines(paste(path, "/iris.csv", sep = ""), n = iv) iris_data # [1] ",Sepal.Length,Sepal.Width,Petal.Length,Petal.Width,Species" "1,5.1,3.5,one.iv,0.two,setosa" # [three] "two,4.nine,three,1.four,0.2,setosa" "three,4.7,3.two,ane.3,0.two,setosa"

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

Example iv: readLines from xlsx Excel File into R?!

In the previous Example, I have shown you how to read csv Excel files with the readLines role. At present you might ask: is it also possible to use readLines for xlsx Excel files?

Reply: As far equally I know, it is not.

Fortunately, there is an easy work-around in example you lot desire to apply readLines to xlsx files – Just convert your xlsx file to csv!

I'm going to bear witness you how:

Starting time, we demand to install and load the xlsx R parcel:

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

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

And then, we tin can employ the write.xlsx function to create an xlsx file for our example (we are using the iris data set once again):

                # 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 have an xlsx file with the proper noun iris_xlsx in your working directory.

At present, we tin can apply the following R code in order to convert 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 = 1) write.csv2(iris_xlsx, paste(path, "/iris_converted.csv", sep = ""))

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

After the conversion, you tin can merely utilize readLines, as I have shown you lot in Instance 3.

Piece of cake informal!

Example 5: Skip Start Lines via n.readLines [reader Package]

Another quite common scenario is that y'all are interested of some lines inside your text, i.e. you desire to skip the offset northward lines and possibly besides the terminal north lines.

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

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

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

We could also use the due north.readLines function to produce the aforementioned output equally we did with readLines of base R in Instance ane:

                # Apply north.readLines function                n.                readLines                (paste(path,                "/my_txt.txt", sep                =                ""                ),             header                =                Imitation,             n                =                3                )                # "this is the offset line"  "this is the second line" "this is the third line"              

# Apply n.readLines function n.readLines(paste(path, "/my_txt.txt", sep = ""), header = Faux, due north = 3) # "this is the starting time line" "this is the 2d line" "this is the third line"

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

                # Employ n.readLines function with skip option                north.                readLines                (paste(path,                "/my_txt.txt", sep                =                ""                ),             header                =                Simulated,             due north                =                2,             skip                =                1                )                # "this is the second line" "this is the third line"              

# Apply due north.readLines office with skip option due north.readLines(paste(path, "/my_txt.txt", sep = ""), header = Simulated, north = two, skip = 1) # "this is the second line" "this is the 3rd line"

We have used n = ii in gild to print 2 lines and nosotros have specified skip = 1 in society to skip the first line.

Instance half dozen (Video): readLines vs. readline – What's the difference?

Ofttimes confusing: Base R provides a function that is called readLines (with upper case L and an s at the cease) and a function that is called readline (all in lower case and no s at the finish).

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 difficult to explain an interactive function in a written tutorial. However, fortunately the YouTube channel Docworld Academy has created a uncomplicated video on the usage of readline in R.

Have fun with the video and let me know in the comments, in case you have whatsoever questions.

Further Reading

  • The R Programming Language

turnertriblend.blogspot.com

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

Postar um comentário for "Skip Reading Number of Lines at the End in R"