[R] apply a function down each column

Steve Lianoglou mailinglist.honeypot at gmail.com
Mon Jan 11 15:18:41 CET 2010


Hi,

On Mon, Jan 11, 2010 at 8:41 AM, Laetitia Schmid <laetitia at gmt.su.se> wrote:
> Hello World,
> I have a function that makes pairwise comparisons between two strings. I would like to apply this function to my data (which consists of columns with different strings) in the way that it compares the first with the second entry, and then the third with the fourth, and then the fifth with the sixth, and so on down each column...
> So (2x-1) and (2x) would be the different entries to be compared!
>
> dat= my data:
>
> for the first column: compare dat[(2x-1),1] with dat[(2x),1] and x would be 1:i, i=length(dat[,1])
>
> I think the best way to do that is a loop:
>
> a <- as.character(dat[(2x-1),1])
> b <- as.character(dat[(2x),1])
>
> for (i in 1:length(dat[,1]) my_function(a, b))
>
> Can somebody help me to apply a function with a loop in the way I want to a column?

It seems as if you got it already, don't you?

for (x in 1:(nrow(dat)-1)) {
  a <- dat[(2x-1),1]
  b <- dat[(2x), 1]
  my_function(a,b)
}

> Is there a specification of "tapply" for that?

I don't think so, but depending on what you want to do, the size of
your data, and the amount of RAM you have, it might be faster to
compare everything "at once" (assuming `my_function` can be
vectorized), for instance:

a <- dat[seq(1, nrow(dat), by=2),1]
b <- dat[seq(2, nrow(dat), by=2), 1]
all.results <- my_function(a,b)

Also, as an aside, I see you keep calling "as.character" on your data
when you extract it from your data.frame. Is your data being converted
to factors? You can look to set stringsAsFactors=FALSE if this is the
case and you are reading in data using read.table/delim/etc (see:
?read.table)

Hope that helps,

-steve

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact



More information about the R-help mailing list