1

I have two data frames:

>dfMapping
Name Number
a    10
b    20
c    15


>dfMapping2
Name Total Type 
a    112    x
a    20     y
a    1      z
b    10     x
b    120    z
c    11     z
c    100    x

From dfMapping2 I want only those rows where dfMapping2$Total > dfMapping$Number and the final Total should have Total - dfMapping$Number. So, here the result should be:

>result
Name  Total  Type
a     102    x
a     10     y
b     100    z
c     85     x

Can someone help me on this?

Thanks.

2
  • 1
    two steps?: 1) merge on Name to make DF 2)DF[DF$Total>DF$Number,] Commented Jan 10, 2014 at 15:16
  • Are you sure the result you show is correct? Commented Jan 10, 2014 at 15:23

1 Answer 1

1

This is Stephan's solution. Step 1: merge dataframe. Step 2: filter based on condition.

df <- merge(dfMapping, dfMapping2)
df <- with(df, df[Total > Number,])

Which generates:

  Name Number Total Type
1    a     10   112    x
2    a     10    20    y
5    b     20   120    z
7    c     15   100    x
Sign up to request clarification or add additional context in comments.

1 Comment

Just two lines: df[,3] <- (df[,3]-df[,2]) to rest the values and then df <- df[,-2] to get just the Name Total and Type columns

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.