Read Only a Specific Line and Csv

readLines, north.readLines & readline in R (vi Example Codes)

In this tutorial, I'm going to show yous how to read text past line with three different R functions:

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

Let'due south first with the basic R syntax of these three functions and some definitions:

Basic R Syntax:

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

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

The readLines function reads text lines from an input file.

The due north.readLines role 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 last.

In order to get a bit more concrete, permit'south motility on to the examples…

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

When y'all take to do text mining / text analysis of larger texts, you will 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 graphic symbol objects for each of the lines.

For the first example, I'm going to create a unproblematic txt file that we can use for the awarding of readLines. In case you desire to reproduce the instance, simply re-create and paste the post-obit code.

So, permit'south first store the directory, where we want to store and load our example information…

                # Store currently used directory                path                <-                getwd(                )              

# Store currently used directory path <- getwd()

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

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

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

If yous 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 ane: Text File for the Application of readLines().

Now, we can employ 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 third line"              

# Apply readLines role to txt file my_txt <- readLines(paste(path, "/my_txt.txt", sep = "")) my_txt # "this is the first line" "this is the 2nd line" "this is the third line"

The output of the function is a vector that contains 3 character strings, i.east. this is the kickoff line, this is the second line, and this is the third line.

As you can come across, we read the whole txt file into R. Easy – Only what if we want to read only certain lines from our text file?

Case ii: Read Starting time n Lines Only

Quite often you will exist interested in the showtime n lines of your input file. Fortunately the readLines R function provides an northward-selection, which lets you specify the number of lines to read.

Nosotros can simply adjust our code as follows…

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

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

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

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

Example 3: readLines from CSV File into R

In this instance, I'm going to apply the readLines R office to read a information frame that is stored in a .csv file.

Let's first 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                =                FALSE                )              

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

If you have a look at the currently used folder on your computer, you lot will find the Iris data prepare. The first few rows of the information wait as follows:

nrow function in R - Iris Example Data Frame

Table ane: First vi Rows of Iris Data Ready.

We tin apply the readLines part to this csv every bit nosotros did before:

                # Apply 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,i.4,0.2,setosa"                # [three] "2,4.9,three,1.4,0.two,setosa"                                     "iii,4.7,3.ii,ane.iii,0.ii,setosa"              

# Utilize readLines function to csv file iris_data <- readLines(paste(path, "/iris.csv", sep = ""), n = 4) iris_data # [ane] ",Sepal.Length,Sepal.Width,Petal.Length,Petal.Width,Species" "1,5.1,3.5,1.4,0.2,setosa" # [iii] "2,4.nine,iii,ane.4,0.2,setosa" "3,4.7,3.ii,1.three,0.2,setosa"

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

Example four: 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. At present you might ask: is it also possible to use readLines for xlsx Excel files?

Reply: As far as I know, information technology is non.

Fortunately, in that location is an easy work-around in example you want to use readLines to xlsx files – Simply convert your xlsx file to csv!

I'thousand going to show you how:

First, we need to install and load the xlsx R packet:

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

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

Then, nosotros tin can use the write.xlsx role to create an xlsx file for our example (we are using the iris information 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 point you should have an xlsx file with the name iris_xlsx in your working directory.

Now, nosotros can utilize the post-obit R code in society to convert the xlsx file to csv:

                # 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                =                ""                )                )              

# 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 bespeak yous should accept a csv file with the name iris_converted in your working directory.

Subsequently the conversion, you lot can just apply readLines, as I have shown you in Case 3.

Easy breezy!

Case 5: Skip Kickoff Lines via n.readLines [reader Package]

Another quite common scenario is that you lot are interested of some lines within your text, i.e. y'all desire to skip the first n lines and possibly too the last n lines.

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

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

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

Nosotros could also use the n.readLines function to produce the same output as we did with readLines of base R in Case 1:

                # Employ n.readLines function                n.                readLines                (paste(path,                "/my_txt.txt", sep                =                ""                ),             header                =                FALSE,             northward                =                3                )                # "this is the offset line"  "this is the second line" "this is the tertiary line"              

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

However, the n.readLines role provides an additional skip-selection:

                # Utilise n.readLines office with skip choice                n.                readLines                (paste(path,                "/my_txt.txt", sep                =                ""                ),             header                =                Imitation,             n                =                2,             skip                =                1                )                # "this is the second line" "this is the third line"              

# Employ n.readLines function with skip choice northward.readLines(paste(path, "/my_txt.txt", sep = ""), header = Fake, n = two, skip = one) # "this is the second line" "this is the tertiary line"

We have used due north = 2 in order to print 2 lines and we accept specified skip = one in society to skip the first line.

Example 6 (Video): readLines vs. readline – What'south the departure?

Oftentimes confusing: Base R provides a office that is called readLines (with upper case 50 and an s at the end) and a function that is called readline (all in lower example and no south at the stop).

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 panel).

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 allow me know in the comments, in example y'all have whatsoever questions.

Further Reading

  • The R Programming Language

cardenasevied1952.blogspot.com

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

0 Response to "Read Only a Specific Line and Csv"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel