[R] replace some values of a column with diffrent values

Pete Brecknock Peter.Brecknock at bp.com
Mon Feb 6 03:27:41 CET 2012


Hi Valerie

One way would be to use the match function.

# Your Data
u =data.frame(coe=c(0,0,0,0,0,0,0,0),
             
name=c("Time","Poten","AdvExp","Share","Change","Accounts","Work","Rating"))

v = data.frame(coeff=c(0.7272727,0.3211112,0.0500123),
               enter=c("Accounts","Time","Poten"))

# Match Function
updates = v[match(u$name,v$enter),"coeff"]
u$coe = ifelse(!is.na(updates), updates, u$coe)   

HTH

Pete


valerie 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 of coeff 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.
> 


--
View this message in context: http://r.789695.n4.nabble.com/replace-some-values-of-a-column-with-diffrent-values-tp4360035p4360249.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list