Re: [R] How do I 'merge' a altered subset of a data.frame back into the same data.frame

From: Marc Schwartz <marc_schwartz_at_me.com>
Date: Fri, 04 Jun 2010 13:39:17 -0500

On Jun 4, 2010, at 1:09 PM, dominik beck wrote:

> Hi
>
> Step 1: I create a data.frame called iolm.
> Step 2: I create a conditional subset i_wtr.
> Step 3: In this subset I add 0.3 to all values in the IOLM_AST column.
> Step 4: Now I am looking for the best way to ‘merge’ the altered subset back
> into the original iolm data.frame
>
> ## STEP 1

>> iolm

> ID IOLM_AST IOLM_AXIS
> 1 1 1.15 165.33
> 2 2 1.20 79.00
> 3 3 0.40 51.66
> 4 4 0.50 57.00
> 5 5 1.77 7.70
> 6 6 0.28 99.70
> 7 7 0.48 160.00
> 8 8 0.74 84.00
> 9 9 1.63 87.00
> 10 10 0.43 150.70
> ....
>
> ## STEP 2
>
>> i_wtr<-subset(iolm,IOLM_AXIS > 60 & IOLM_AXIS < 120)
>> i_wtr

> ID IOLM_AST IOLM_AXIS
> 2 2 1.20 79.0
> 6 6 0.28 99.7
> 8 8 0.74 84.0
> 9 9 1.63 87.0
> 16 16 0.93 94.0
> 20 20 1.37 91.0
> 21 21 1.19 63.0
> ...
>
> ## STEP 3
>
> i_wtr$IOLM_AST <- i_wtr$IOLM_AST + 0.3
>> i_wtr

> ID IOLM_AST IOLM_AXIS
> 2 2 1.50 79.0
> 6 6 0.58 99.7
> 8 8 1.04 84.0
> 9 9 1.93 87.0
> 16 16 1.23 94.0
> 20 20 1.67 91.0
> 21 21 1.49 63.0
> ...
>
> ## STEP 4 – result (not what I wish to get)
>
> newiolm<-merge(i_wtr, iolm, by.x="ID", by.y="ID", all = T)
>> newiolm

> ID IOLM_AST.x IOLM_AXIS.x IOLM_AST.y IOLM_AXIS.y
> 1 1 NA NA 1.15 165.33
> 2 2 1.50 79.0 1.20 79.00
> 3 3 NA NA 0.40 51.66
> 4 4 NA NA 0.50 57.00
> 5 5 NA NA 1.77 7.70
> 6 6 0.58 99.7 0.28 99.70
> 7 7 NA NA 0.48 160.00
> 8 8 1.04 84.0 0.74 84.00
> 9 9 1.93 87.0 1.63 87.00
> 10 10 NA NA 0.43 150.70
> ...
>
>
> ## The result I am looking to get:
>
> ID IOLM_AST IOLM_AXIS
> 1 1 1.15 165.33
> 2 2 1.50 79.00
> 3 3 0.40 51.66
> 4 4 0.50 57.00
> 5 5 1.77 7.70
> 6 6 0.58 99.70
> 7 7 0.48 160.00
> 8 8 1.04 84.00
> 9 9 1.93 87.00
> 10 10 0.43 150.70
> ...
>
> What is the correct way to do this?
> Thanks a lot for your help.
> Dominik

Just replace the values that meet the condition with the new values, leaving the others alone:

> iolm

   ID IOLM_AST IOLM_AXIS

1   1     1.15    165.33
2   2     1.20     79.00
3   3     0.40     51.66
4   4     0.50     57.00
5   5     1.77      7.70
6   6     0.28     99.70
7   7     0.48    160.00
8   8     0.74     84.00
9   9     1.63     87.00
10 10     0.43    150.70


# See ?ifelse and ?with

> with(iolm, ifelse(IOLM_AXIS > 60 & IOLM_AXIS < 120,

                    IOLM_AST + 0.3, 
                    IOLM_AST))

 [1] 1.15 1.50 0.40 0.50 1.77 0.58 0.48 1.04 1.93 0.43

# Replace the original column with the above result

iolm$IOLM_AST <- with(iolm, ifelse(IOLM_AXIS > 60 & IOLM_AXIS < 120, 
                                   IOLM_AST + 0.3, 
                                   IOLM_AST))

> iolm

   ID IOLM_AST IOLM_AXIS

1   1     1.15    165.33
2   2     1.50     79.00
3   3     0.40     51.66
4   4     0.50     57.00
5   5     1.77      7.70
6   6     0.58     99.70
7   7     0.48    160.00
8   8     1.04     84.00
9   9     1.93     87.00
10 10     0.43    150.70


HTH, Marc Schwartz



R-help_at_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. Received on Fri 04 Jun 2010 - 18:42:21 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 04 Jun 2010 - 19:00:28 GMT.

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

list of date sections of archive