Create a gist now

Instantly share code, notes, and snippets.

@MattSandy /story.R
Last active Sep 17, 2016

Creates an ordered frequency count based on input from the terminal. echo "test1 test2 test2" | Rscript story.R
input<-file('stdin', 'r')
rows <- readLines(input, n=1)
story <- c()
story <- unlist(lapply(rows,function(x) {
split <- strsplit(gsub("[[:punct:]]", "",x), " |\t")
return(split)
}))
df <- data.frame(sort(table(story),decreasing=TRUE))
df$Word <- row.names(df)
names(df) <- c("Frequency","Word")
row.names(df) <- NULL
print(df[order(-df$Frequency,df$Word),c("Word","Frequency")],row.names=FALSE)
echo "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ut tortor et diam blandit tristique at eu dolor. Nulla placerat laoreet pulvinar. Cras id enim quis dui scelerisque tincidunt ac nec augue. Pellentesque vulputate vulputate condimentum. Suspendisse magna urna, lacinia sed diam eget, tempor viverra est. Praesent tristique sagittis cursus. Nullam volutpat pellentesque arcu ut finibus. Pellentesque euismod eleifend accumsan. Nunc libero eros, sodales at tortor eget, luctus commodo metus. Nam laoreet, arcu lobortis mollis volutpat, leo elit sollicitudin neque, ut rhoncus dolor nisl non quam. Vivamus consequat, ligula in finibus consequat, erat magna suscipit augue, vel volutpat tortor velit sed diam. Pellentesque dui est, suscipit eu sodales vel, sagittis eget mauris. Nunc pharetra leo eget tellus bibendum aliquam. Nam sit amet dignissim lorem, a dapibus lorem. Vestibulum nec ante dui." | story.R
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment