[R] merge dataframes with conditions formulated as logical expressions

From: Wolfram Fischer <wolfram_at_fischer-zim.ch>
Date: Thu 15 Jun 2006 - 00:55:07 EST


I have a data.frame df containing two variables:

    GRP: Factor
    VAL: num

I have a data.frame dp containing:

    GRP: Factor
    MIN.VAL: num
    MAX.VAL: num
    VAL2: num
with several rows per "GRP"
where dp[i-1, "MAX.VAL"] < dp[i, "MIN.VAL"] within the same "GRP".

I want to create df[i, "VAL2"] <- dpp[z, "VAL2"] with i along df
and dpp <- subset( dp, GRP = df[i, "GRP"] ) so that it is true for each i:

        df[i, "VAL"] > dpp[z, "MIN.VAL"]    and df[i, "VAL"] <= dpp[z, "MAX.VAL"]

Is there an easy/efficient way to do that?

Example:
df <- data.frame( GRP=c( "A", "A", "B" ), VAL=c( 10, 100, 200 ) ) dp <- data.frame( GRP=c( "A", "A", "B", "B" ),

    MIN.VAL=c( 1, 50, 1, 70 ), MAX.VAL=c( 49, 999, 59, 999 ),     VAL2=c( 1.1, 2.2, 3.3, 4.4 ) )

The result should be:

    df$VAL2 <- c( 1.1, 2.2, 4.4 )

Thanks - Wolfram



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Jun 15 01:04:32 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 15 Jun 2006 - 04:11:16 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.