indexing - Is there an R function for finding the index of an element in a vector?

The function match works on vectors : x <- sample(1:10) x # [1] 4 5 9 3 8 1 6 10 7 2 match(c(4,8),x) # [1] 1 5 match only returns the first encounter of a match, as you requested. It return... Read More

r - Calculate row means on subset of columns

Calculate row means on a subset of columns: Create a new data.frame which specifies the first column from DF as an column called ID and calculates the mean of all the other fields on that row, and pu... Read More

r - Concatenate a vector of strings/character

Try using an empty collapse argument within the paste function: paste(sdata, collapse = '') Thanks to http://twitter.com/onelinetips/status/7491806343... Read More

r - How to stop emacs from replacing underbar with <- in ess-mode

From ESS's manual (look under "Changes/New Features in 5.2.0"): ESS[S]: Pressing underscore ("_") once inserts " <- " (as before); pressing underscore twice inserts a literal underscore. To stop thi... Read More

r - Generate a dummy-variable

Another option that can work better if you have many variables is factor and model.matrix. > year.f = factor(year) > dummies = model.matrix(~year.f) This will include an intercept column (all ones) a... Read More

r - Select multiple columns in data.table by their numeric indices

For versions of data.table >= 1.9.8, the following all just work: library(data.table) dt <- data.table(a = 1, b = 2, c = 3) # select single column by index dt[, 2] # b # 1: 2 # select multiple c... Read More

Java-R integration?

I have successfully used two alternatives in the past. JRI Pros : probably better performance. Cons : you have to configure some environment variables and libraries, different in Win/UNIX. RServe... Read More

r - How to trim leading and trailing whitespace?

As of R 3.2.0 a new function was introduced for removing leading/trailing whitespaces: trimws() See: http://stat.ethz.ch/R-manual/R-patched/library/base/html/trimws.html... Read More

installation - Installing older version of R package

To install an older version of a package from source (within R): packageurl <- "http://cran.r-project.org/src/contrib/Archive/ggplot2/ggplot2_0.9.1.tar.gz" install.packages(packageurl, repos=NULL, ty... Read More

statistical finance - What are the advantages of financial modelling in R?

It is very hard to answer this quiz as people might be good at different at tools. For example, if you are good at VBA, then you can achieve the same effect compared to R in most cases. The following... Read More