[R] How to replace the values in a column

ilai keren at math.montana.edu
Wed Feb 29 07:06:15 CET 2012


Hannah,
If Gen is a factor you can simply build the new factor "on top" of it:

dataframe$Gen<- factor( c('Wynda' , 'A_2' , 'B_1' , 'Wynda' , 'Wynda'
, 'OP1_5')[Gen] )

Just make sure the replacement labels are in the same order as levels(Gen).

Cheers

On Tue, Feb 28, 2012 at 8:39 PM, hannahmaohuang
<hannahmaohuang at gmail.com> wrote:
> Dear All,
> I've been searching relevant topics about replacing values, none seemed to
> be applicable to me...
>
> I have a file with many many varieties, and want to replace some of them
> into different names.
> I tried various of ways, still don't know how to do that most efficiently..
> Here is part of the example data:
>
>
> Gen    Rep
> A_1      1
> A_1       2
> A_2     1
> A_2     2
> B_1       1
> B_1       2
> B_3       1
> B_3       2
> OP1_1   1
> OP1_1   2
> OP1_5   1
> OP1_5   2
>
> For example, I want to replace  A_1,  B_3,  OP1_1 into different name
> "Wynda"
>
> So that the expected file should become:
>
> Gen          Rep
> Wynda      1
> Wynda        2
> A_2           1
> A_2           2
> B_1              1
> B_1             2
> Wynda        1
> Wynda        2
> Wynda        1
> Wynda        2
> OP1_5          1
> OP1_5         2
>
>
> I have created a link file, which contains two rows, translating which Gen
> correlating to which Name. Not sure if this file helps or not, example as
> below:
>
> Column1(Gen)        Column2(Name)
> A_1                               Wynda
> A_2                                 A_2
> B_1                                 B_1
> B_3                             Wynda
> OP1_1                       Wynda
> OP1_5                        OP1_5
>
>
> Though I can replace one by one in excel, since there are too many files and
> too many reps, it'll be very time-consuming also easy to make mistakes.
>
> Please give me any guidance or help in terms of finish this with R.
>
> Thanks so much !
>
> Sincerely
> Hannah
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-replace-the-values-in-a-column-tp4430448p4430448.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