Learned from an email thread. This is the kind of problem you'll never think about if you are a student using R. Suppose you have a k by n matrix, say x, where k is small but n is huge (say 1,000,000), and you want to get the maximum in each column, that is, n different values.
The naive way of apply(x, 2, max) takes forever to complete.
The new trick I learned is:
# transpose x and convert to a data.frame
t.x <- as.data.frame(t(x))
# use do.call with "pmax" and the input matrix
x.max <- do.call("pmax", t.x)
Showing posts with label r. Show all posts
Showing posts with label r. Show all posts
Saturday, November 21, 2009
Wednesday, July 23, 2008
debugging R
These two functions could be used in debugging the code. Avoid using browser in a big loop, see help file for more detailed information.
if(v0<=0) browser()
debug(func)
Thursday, September 27, 2007
Using C functions in R
Method:
convert .c file to shared library .so via link ready object file .o, then use dyn.load() in R to call the shared library, and use .C() to convert it to R function.
Example 1, conv
Feature: simple c example without external library.
1.1 C code:
1.2 Terminal Process:
1.3 R code:
Example 2, Exp1
Feature: c example with gsl library.
2.1 C code:
2.2 Terminal Process:
2.3 R code:
Example 3, Exp1 in C++
Feature: C++ example with gsl library.
How: just wrap all functions in extern “C”{}.
3.1 C code:
3.2 Terminal Process:
3.3 R code:
convert .c file to shared library .so via link ready object file .o, then use dyn.load() in R to call the shared library, and use .C() to convert it to R function.
Example 1, conv
Feature: simple c example without external library.
1.1 C code:
/* conv.c */
void convolve(double *a, int *na,
double *b, int *nb, double *ab){
int i, j, nab=*na + *nb -1;
for (i=0; i<nab; i++)
ab[i] = 0.0;
for (i=0; i<*na; i++)
for (j=0; j<*nb; j++)
ab[i+j] = ab[i+j] + a[i]*b[j];
1.2 Terminal Process:
gcc -c conv.c
R CMD SHLIB conv.o
R CMD SHLIB conv.c
1.3 R code:
# conv.r
dyn.load("conv.so");
conv <- function(a, b){ .C("convolve", as.double(a), as.integer(length(a)), as.double(b), as.integer(length(b)), ab = double(length(a)+length(b)-1))$ab } x <- 1:3 y <- 1:5 z <- conv(x,y)
Example 2, Exp1
Feature: c example with gsl library.
2.1 C code:
/* exp1.c */
#include
void myexp(double *x, int *nx, double *y){
int i;
for (i=0; i<*nx; i++){ y[i] = gsl_sf_expint_E1(x[i]); } }
2.2 Terminal Process:
gcc -c exp1.c
gcc -shared -o exp1.so exp1.o -lgsl -lblas -lm
R CMD SHLIB exp1.o /usr/lib/libgsl.a
2.3 R code:
# exp1.r
dyn.load("exp1.so");
myexp <- function(x){
.C("myexp", as.double(x), as.integer(length(x)),
y = double(length(x)))$y;
}
myexp(1:4);
Example 3, Exp1 in C++
Feature: C++ example with gsl library.
How: just wrap all functions in extern “C”{}.
3.1 C code:
/* exp1.cpp */
#include <gsl/gsl_sf_expint.h>
extern “C”{
void myexp(double *x, int *nx, double *y){
int i;
for (i=0; i<*nx; i++){ y[i] = gsl_sf_expint_E1(x[i]); } } }
3.2 Terminal Process:
g++ -c exp1.cpp
g++ -shared -o exp1.so exp1.o -lgsl -lblas -lm
R CMD SHLIB exp1.o /usr/lib/libgsl.a
3.3 R code:
# exp1.r
dyn.load("exp1.so");
myexp <- function(x){ .C("myexp", as.double(x), as.integer(length(x)), y = double(length(x)))$y; } myexp(1:4);
Install R packages locally
在没有权限写R的安装目录的时候,可以在user的目录下局部安装R的软件包,比较方便的做法是如下四步(如果已经完成了前两步,就只需执行最后两步):
- 在$HOME下建立一个文件夹存放自己的R package:
cd
mkdir .Rlibs - 在$HOME下建立环境文件.Renviron,使得每次启动R的时候自动识别package所在的目录文件。文件.Renviron只有如下一行文字:
R_LIBS=$HOME/.Rlibs
- 下载相应的软件包,比如 gsl_1.6.6.tar.gz
- 在下载目录下安装:
R CMD INSTALL -l $HOME/.Rlibs gsl
R # and run next line inside R
install.packages("packagename", lib=LIBPATH)
Subscribe to:
Posts (Atom)