Skip to content
Levi Brackman (Rabbi, PhD)

Scholar, Podcaster. Author, Seeker, Social Scientist, Entrepreneur

Levi Brackman (Rabbi, PhD)

Scholar, Podcaster. Author, Seeker, Social Scientist, Entrepreneur

Function in R for Word and Line Count Table

Levi Brackman, May 1, 2016

Here I present a new function I created to find the count of lines and words in a text document and return them in the form of a table. It uses the wc “qdap” package in R as well as base R functions sum, nrow, as.numeric, as.data.frame and cbind.

The Problem:

How to find both the number of lines and the amount of words in a potentially large document using R and return it as a table”

The solution:

First install and load qdap package

[code lang=”r”]install.packages("qdap");library(qdap)[/code]

Load text document

[code lang=”r”]doc = readLines("doc.txt", ok = TRUE)[/code]

Read “WordsLines” in Function

[code lang=”r”]
WordsLines = function(dataframe, names1, names2){
Words = as.data.frame(dataframe) #since the dataframe is in text format put it into a dataframe
Wc = wc(Words[,1]) #get the word count of each input (all rows) of the first column
Words1 = as.data.frame(Wc) #put that word count into a dataframe
Words1$Wc = as.numeric(Words1$Wc) #make sure it is numeric
names(Words1)[1] = paste("Words") #change the column name to "Words"
Words1 = sum(Words1, na.rm = T) #Sum all the word counts of the entire column
Lines = nrow(Words) #find the number of words in the entire dataframe
final = cbind(Lines, Words1) #combine the line count and wort count into one table
colnames(final) = c(names1, names2) #change the names of the columns to fit the particular dataset
final #return the table
}
[/code]

Call function

[code lang=”r”]WordsLines(doc, "Doc Lines", "Doc Words")[/code]

Should return something like this:

[code lang=”r”]
Doc Lines Doc Words
[1,] 1010242 33482314[/code]

Code Data Science R Science Data Science

Post navigation

Previous post
Next post

Related Posts

Enterprise-Level Data Science: Lessons From the Frontlines

February 28, 2019September 2, 2019

(First published by datascience.com) Data science, machine learning (ML) and artificial intelligence (AI) are relatively new endeavors for enterprise-level business. Many companies are batch training as well as batch scoring ML models. Predictions are stored in a database to be retrieved either by applications or humans. However, real-time training on…

Read More

Why I love Las Vegas: The Law of Large Numbers

March 25, 2016March 25, 2016

(View the app I created that illustrates the Law of Large Numbers either before or after reading this article here) Las Vegas is caters to people’s vices of all kinds. Sheindy and I love visiting Sin City although we do not gamble, drink or partake in any of the other…

Read More

Polls, Margin of Errors and Standard Deviations

April 16, 2016April 16, 2016

See My App that Explains Standard Deviations Intuitively Here This coming week there are big primaries with lot of delegates up for grabs in New York. It seems from the polls that the both Trump and Clinton are ahead. How reliable are those polls? There are many ways to answer…

Read More

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

About Me and this Site

  • Navigating My Writing
  • Short Bio
  • Join Newsletter

Three Latest Posts

  • Before Bezos and Amazon: The Biblical Covenantal Relationship Had an Inbuilt ‘Day One’ Mindset September 20, 2024
  • Rabbinic Reinterpretation of the Torah: Ethical Adjustments in the Oral Law – Ki Teitzei September 16, 2024
  • Navigating Nepotism and Divine Choice in Torah Portion Tsav March 29, 2024

Writing in Chronological Order

Writing by Category

Search the Site

©2025 Levi Brackman (Rabbi, PhD) | WordPress Theme by SuperbThemes