[R] Replacing values in dataframes

Don MacQueen macq at llnl.gov
Sun Sep 20 06:35:31 CEST 2009


What I would probably do is along these lines:

   iddf <- data.frame(Sample.id=names(Prot.amount), new.id=Prot.amount[1,])

   newNAD <- merge( NAD, iddf)

This is not tested, but it looks right to me, 
assuming I understand the structure of what 
you're trying to do.

I'm also assuming that NAD has more than three 
rows, and that Prot.amount has as many columns as 
NAD has rows. And that you just showed us the 
first three rows of NAD and first three columns 
of Prot.amount in order to keep the email simple.

One final note ... if Prot.amount is an object 
within R, it is *not* a file. You may have read 
it in from a file, of course, but it isn't a file 
inside R. I'm assuming it's a dataframe.

-Don

At 1:18 PM +0300 9/19/09, Monna Nygård wrote:
>Hi,
>
>
>
>This is a question of a newbie getting into the exciting world of R.
>
>
>
>I have several dataframes in the same format as NAD:
>
>
>
>
>
>>  NAD[1:3,1:3]
>
>     Sample.Id Main.abs..1 Main.abs..2
>148       10a     0.04836     0.04994
>167  11a_1109     0.32245     0.36541
>173  11b_1109     0.29293     0.32815
>
>
>What I want to do is to replace the Sample.Id 
>with a corresponding number.The number i have in 
>another file,called Prot.amount
>
>
>
>>  Prot.amount[1:3,1]
>      10a 11a_1109 11b_1109
>   15.516   38.248   42.297
>
>
>
>
>
>>  row.names(NAD)<-(NAD[,1])
>>  NAD$Sample.Id <- replace(NAD$Sample.Id, 
>>NAD$Sample.Id=="10a",Prot.amount["10a",1])
>
>>  NAD[1:3,1:3]
>          Sample.Id Main.abs..1 Main.abs..2
>10a         15.516     0.04836     0.04994
>11a_1109  11a_1109     0.32245     0.36541
>11b_1109  11b_1109     0.29293     0.32815
>
>
>
>So what I have tried to do is to write a 
>function that would allow me to replace the 
>values automatically of all dataframes. This I 
>just can't get to work. 
>
>
>
>Thank you so much in advance!
>  			  		 
>_________________________________________________________________
>[[elided Hotmail spam]]
>
>	[[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.


-- 
---------------------------------
Don MacQueen
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062
macq at llnl.gov




More information about the R-help mailing list