Last active
September 17, 2016 19:47
-
-
Save MattSandy/2960a4e48f6875d3eae2 to your computer and use it in GitHub Desktop.
Creates an ordered frequency count based on input from the terminal. echo "test1 test2 test2" | Rscript story.R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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