[R] replace some values of a column with diffrent values
Petr PIKAL
petr.pikal at precheza.cz
Mon Feb 6 08:25:42 CET 2012
Hi
For such tasks I would use merge
merge(u, v, by.x="name", by.y = "enter", all.x=TRUE)
If you do not want coe column anymore you can easily get rid of it.
Regards
Petr
>
> 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.
>
> ______________________________________________
> 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