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

How to convert a factor to integer\numeric without loss of information?

See the Warning section of ?factor: In particular, as.numeric applied to a factor is meaningless, and may happen by implicit coercion. To transform a factor f to approximately its original numeric values, as.numeric(levels(f))[f] is recommended and slightly more efficient than as.numeric(as.character(f)). The FAQ on R has similar advice. Why is as.numeric(levels(f))[f] more efficent than as.numeric(as.character(f))? … Read more

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