Quickly reading very large tables as dataframes

An update, several years later This answer is old, and R has moved on. Tweaking read.table to run a bit faster has precious little benefit. Your options are: Using vroom from the tidyverse package vroom for importing data from csv/tab-delimited files directly into an R tibble. See Hector’s answer. Using fread in data.table for importing … Read more

Create an empty data.frame

Just initialize it with empty vectors: df <- data.frame(Date=as.Date(character()), File=character(), User=character(), stringsAsFactors=FALSE) Here’s an other example with different column types : df <- data.frame(Doubles=double(), Ints=integer(), Factors=factor(), Logicals=logical(), Characters=character(), stringsAsFactors=FALSE) str(df) > str(df) ‘data.frame’: 0 obs. of 5 variables: $ Doubles : num $ Ints : int $ Factors : Factor w/ 0 levels: $ Logicals … Read more

Test if a vector contains a given element

Both the match() (returns the first appearance) and %in% (returns a Boolean) functions are designed for this. v <- c(‘a’,’b’,’c’,’e’) ‘b’ %in% v ## returns TRUE match(‘b’,v) ## returns the first location of ‘b’, in this case: 2

How to unload a package without restarting R

Try this (see ?detach for more details): detach(“package:vegan”, unload=TRUE) It is possible to have multiple versions of a package loaded at once (for example, if you have a development version and a stable version in different libraries). To guarantee that all copies are detached, use this function. detach_package <- function(pkg, character.only = FALSE) { if(!character.only) … Read more

The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe

The R Language Definition is handy for answering these types of questions: http://cran.r-project.org/doc/manuals/R-lang.html#Indexing R has three basic indexing operators, with syntax displayed by the following examples x[i] x[i, j] x[[i]] x[[i, j]] x$a x$”a” For vectors and matrices the [[ forms are rarely used, although they have some slight semantic differences from the [ form … Read more

What is the difference between require() and library()?

There’s not much of one in everyday work. However, according to the documentation for both functions (accessed by putting a ? before the function name and hitting enter), require is used inside functions, as it outputs a warning and continues if the package is not found, whereas library will throw an error.

How can I view the source code for a function?

UseMethod(“t”) is telling you that t() is a (S3) generic function that has methods for different object classes. The S3 method dispatch system For S3 classes, you can use the methods function to list the methods for a particular generic function or class. > methods(t) [1] t.data.frame t.default t.ts* Non-visible functions are asterisked > methods(class=”ts”) … Read more

How should I deal with “package ‘xxx’ is not available (for R version x.y.z)” warning?

1. You can’t spell The first thing to test is have you spelled the name of the package correctly? Package names are case sensitive in R. 2. You didn’t look in the right repository Next, you should check to see if the package is available. Type setRepositories() See also ?setRepositories. To see which repositories R … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)