Hi Dimitri,
 
Does this help?
 
k1<-data.frame(item=sample(rep(letters),10,replace=T),a=c(1:10),b=11:20)
k2<-data.frame(item="f",a=3,b=10)
merge<-function(y,x)
{
if(y$a>min(x$a))
{
  x<-rbind(x,y)
  x<-x[-which.min(x$a),]
}
return(x)
}
merge(k2,k1)
 
or much faster way would be to refer "library(sqldf)".

--- On Wed, 30/1/13, Dimitri Liakhovitski <dimitri.liakhovit...@gmail.com> 
wrote:


From: Dimitri Liakhovitski <dimitri.liakhovit...@gmail.com>
Subject: [R] Fastest way to compare a single value with all values in one 
column of a data frame
To: "r-help" <r-help@r-project.org>
Date: Wednesday, 30 January, 2013, 2:41 AM


Hello!

I have a large data frame x:
x<-data.frame(item=letters[1:5],a=1:5,b=11:15)  # in actuality, x has 1000
rows
x$item<-as.character(x$item)
I also have a small data frame y with just 1 row:
y<-data.frame(item="f",a=3,b=10)
y$item<-as.character(y$item)

I have to decide if y$a is larger than the smallest of all the values in
x$a. If it is, I want y to replace the whole row in x that has the lowest
value in column a.
This is how I'd do it.

if(y$a>min(x$a)){
  whichmin<-which(x$a==min(x$a))
  x[whichmin,]<-y[1,]
}


I am wondering if there is a faster way of doing it. What would be the
fastest possible way? I'd have to do it, unfortunately, many-many times.

Thank you very much!

-- 
Dimitri Liakhovitski
gfk.com <http://marketfusionanalytics.com/>

    [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to