background preloader R-project Search Engine R-project Search Engine
Related:  Learning R

The Guerilla Guide to R Update: Okay. I’ve uploaded a new template and things seem to be fine now. Update: I am aware the table of contents is not being displayed in bullet form as I intended. The web template I’m using seems to be buggy. It also seems to think this page is in Indonesian…Working on it! Table of Contents: About: Stack Overflow is awesome. This is why I’ve collated, The Guerilla Cookbook for R. The cool thing is, this “book” essentially writes itself since most of the experts (and peer-reviewers) are answering the questions. How Was The Content Selected? I personally searched through Stack Overflow to find my favorite questions and shared them here.

Google Dev R Video Lectures I got this Google Developers R Programming Video Lectures from Stephen's blog - Getting Genetics Done. Very useful R tutorial for beginner! Short and efficient. Here is what I learned after watching the lectures: stop() and warning() function I was asked this question during a job interview. stop('message') will print out the error message and stop the function. warning('message') will print out the error message but continue the function. Passing additional arguments using an ellipsis(...), for example: myFunc <- function(a, return colMean(a, will allow us to call the function like myFun(a, na.rm=T), for example. You can also record the ellipsis arguments by args=list(...) return() vs. invisible() return() will return the values and print out in the screen. invisible will return the values but not print out to the screen. use recall() to call recursive function in R.

Guide to Speeding Up R Code This is an overview of tools for speeding up your R code that I wrote for the Davis R Users’ Group. First, Ask “Why?” It’s customary to quote Donald Knuth at this point, but instead I’ll quote my twitter buddy Ted Hart to illustrate a point: I’m just going to say it.I like for loops in #Rstats, makes my code readable.All you [a-z]*ply snobs can shove it! — Ted Hart (@DistribEcology) March 12, 2013 Code optimization is a matter is a matter of personal taste and priorities. (1) Do you want your code to be readable? If you need to explain your code to yourself or others, or you will need to return to it in a few months time and understand what you wrote, it’s important that you write it in a way that is easy to understand. Some optimal code can be hard to read. (2) Do you want your code to be sharable? Most of the considerations of (1) apply here, but they have to be balanced with the fact that, if your code is painfully slow, others are not going to want or have time to use it. No? compare

Creating your personal, portable R code library with GitHub As I discussed in a previous post, I have a few helper functions I’ve created that I commonly use in my work. Until recently, I manually included these functions at the start of my R scripts by either the tried and true copy-and-paste method, or by extracting them from a local file with the <code>source()</code> function. The former approach has the benefit of keeping the helper code inextricably attached to the main script, but it adds a good bit of code to wade through. The latter approach keeps the code cleaner, but requires that whoever is running the code always has access to the sourced file and that it is always in the same relative path – and that makes sharing or moving code more difficult. The resulting approach takes advantage of GitHub Gists and R’s ability to source via a web-based location to enable you to create a personal, portable library of R functions for private use or to share. The process is very straightforward.

Style - Hadley Good coding style is like using correct punctuation. You can manage without it, but it sure makes things easier to read. As with styles of punctuation, there are many possible variations. The following guide describes the style that I use (in this book and elsewhere). It is based on Google’s R style guide, with a few tweaks. Good style is important because while your code only has one author, it’ll usually have multiple readers. The formatR package, by Yihui Xie, makes it easier to clean up poorly formatted code. Notation and naming File names File names should be meaningful and end in .R. # Good fit-models.R utility-functions.R # Bad foo.r stuff.r If files need to be run in sequence, prefix them with numbers: 0-download.R 1-parse.R 2-explore.R Object names “There are only two hard things in Computer Science: cache invalidation and naming things.”— Phil Karlton Variable and function names should be lowercase. # Good day_one day_1 # Bad first_day_of_the_month DayOne dayone djm1 Syntax Spacing

Translating between R and SQL: the basics An introductory comparison of using the two languages. Background R was made especially for data analysis and graphics. SQL was made especially for databases. They are allies. The data structure in R that most closely matches a SQL table is a data frame. A mashup There is an R package called sqldf that allows you to use SQL commands to extract data from an R data frame. it must be installed on your machine (once)it must be available in each R session where it is used You can use sqldf by doing (one time only, and assuming an internet connection): install.packages("sqldf") Then in each R session where you want to use it: require(sqldf) To simplify the examples, we’ll slightly modify one of the inbuilt data frames: myCO2 <- CO2 attributes(myCO2) <- attributes(CO2)[ c("names", "row.names", "class")] class(myCO2) <- "data.frame" Note that the character between C and 2 is a capital-O and not a zero. Column names In R the colnames function returns the names of the columns: Subsetting columns All columns

Memory usage · Advanced R. A solid understanding of R’s memory management will help you predict how much memory you’ll need for a given task and help you to make the most of the memory you have. It can even help you write faster code because accidental copies are a major cause of slow code. The goal of this chapter is to help you understand the basics of memory management in R, moving from individual objects to functions to larger blocks of code. Outline Prerequisites In this chapter, we’ll use tools from the pryr and lineprof packages to understand memory usage, and a sample dataset from ggplot2. install.packages("ggplot2") install.packages("pryr") install.packages("devtools") devtools::install_github("hadley/lineprof") Sources The details of R’s memory management are not documented in a single place. Object size To understand memory usage in R, we will start with pryr::object_size(). library(pryr) object_size(1:10) ## 6.74 kB This isn’t just an artefact of integer vectors. Object metadata (4 bytes). Exercises Loops

Rules for Naming Objects in R How are objects allowed to be named in R? As it turns out, this is a very different question from how should objects be named. This isn't about style conventions, camelCase, dots.verus_underscores, or anything like that; this is about what is strictly possible. I do a lot of outreach to HPC people who are starting to get an interest in R, often because their users are starting to ask for help with R. This is a horrible lie. From ? There are no restrictions on name: it can be a non-syntactic name (see make.names). What does that mean? R simply won't allow it. You can prove that it works by checking your global environment with ls(). We can also do fun things like: Remember when I said you could even use unicode (assuming your terminal supports it)? All perfectly valid. As mentioned above, there are three sets of quotes in R; single and double ticks, i.e. ' and ", generally serve the same purpose of creating character data (which the rest of the world calls strings). Or

How to ask for R help by Stephen Weller, Senior Support Engineer at Revolution Analytics, and Joseph Rickert For someone trying to learn any new technology getting help with a problem on a public forum can be stressful. Knowing where to go, deciding how to pose a question and figuring out how to deal with a response can be challenging. Preliminary WorkTwo of the most novice friendly places to go for help in the R world are the R-Help mailing list on CRAN and the R section of stack overflow. Stack overflow provides some excellent suggestions on posting questions. Sharing your research helps everyone. Posting Your QuestionWhen comes time to post your question you may find Steves guidelines helpful. You should always include information on which version of R and what flavor of operating system you are running (see 'R.version' and 'Revo.version' in R)Be as specific as you can in describing your problem. #1 - a good post call: inDL(x, as.logical(local), as.logical(now), ...) thanks #2 - a good post Thank you, Hi,