[R] how to REPLACE VALUES in a dataframe

Jim Holtman jholtman at gmail.com
Mon Feb 6 05:51:45 CET 2012


indx <- match(u$nam, v$enter, nomatch = 0)
u$coe[indx != 0] <- v$coeff[indx]

Sent from my iPad

On Feb 5, 2012, at 15:45, Valerie Moore <vmoore2020 at yahoo.com> wrote:

> Hi,
> 
> I have two data frames (u and v).
> 
>> u
>   coe      nam
> 1   0     Time
> 2   0    Poten
> 3   0   AdvExp
> 4   0    Share
> 5   0   Change
> 6   0 Accounts
> 7   0     Work
> 8   0   Rating
> 
> 
>> v
>       coeff    enter
> 1 0.7272727 Accounts
> 2 0.3211112     Time
> 3 0.0500123    Poten
> 
> 
> I want to update the values of coe in u by using the values ofcoeff in v.
> That is, I want to get the following result
> 
>> u
>   coe                   nam
> 1   0.3211112     Time
> 2   0.0500123    Poten
> 3   0                   AdvExp
> 4   0                   Share
> 5   0                   Change
> 6    0.7272727   Accounts
> 7   0                   Work
> 8   0                   Rating
> 
> 
> OR the following result is also acceptable:
> 
> 0.3211112    0.0500123    0    0    0     0.7272727    0    0
> 
> 
> I tried the following, but the result is not right.
> replace(coe,colnames,coeff)
> [1] 0.7272727 0.0500123 0.3211112 0.0000000 0.0000000 0.0000000 0.0000000
> [8] 0.0000000
> 
> 
> PLEASE HELP ME. 
> 
> THANK YOU VERY MUCH.
> 
> 
> 
> 
> ~valerie
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.



More information about the R-help mailing list