Re: [R] How to replace NAs in a vector of factors?

2009-07-22 Thread Gene Leynes
Thank you so much, I'm humbled by the response from such great authors and scholars. I thought I would share the final version that worked perfectly in my illustrative example, as well as the real one. My main confusion was this part: db1$olditems[db1$olditems==''] [1] Levels: nuts soup I

[R] How to replace NAs in a vector of factors?

2009-07-21 Thread Gene Leynes
# Just when I thought I had the basic stuff mastered # This has been quite perplexing, thanks for any help ## Here's the example: db1=data.frame( olditems=c('soup','','','','nuts'), prices=c(4.45, 3.25, 4.42, 2.25, 3.98)) db2=data.frame(

Re: [R] How to replace NAs in a vector of factors?

2009-07-21 Thread hadley wickham
On Tue, Jul 21, 2009 at 7:39 PM, Gene Leynesgleyne...@gmail.com wrote: # Just when I thought I had the basic stuff mastered # This has been quite perplexing, thanks for any help ## Here's the example: db1=data.frame(    olditems=c('soup','','','','nuts'),    prices=c(4.45, 3.25, 4.42,

Re: [R] How to replace NAs in a vector of factors?

2009-07-21 Thread jim holtman
Notice that three items are returned where you thought one was: [1] FALSE TRUE TRUE TRUE FALSE db1$olditems[db1$olditems==''] #wait, only one item is returned? [1] Levels: nuts soup db1[db1$olditems=='',] #somehow this works! olditems prices 23.25 34.42 4

Re: [R] How to replace NAs in a vector of factors?

2009-07-21 Thread Bill.Venables
Couple of points: 1. if you are going to be replacing entries in factors with updated levels, it's probably easier if you start with your strings remaining as strings as they go into the data frames. So here is how I would start your example db1 - data.frame( olditems =

Re: [R] How to replace NAs in a vector of factors?

2009-07-21 Thread Rolf Turner
On 22/07/2009, at 1:21 PM, jim holtman wrote: Notice that three items are returned where you thought one was: [1] FALSE TRUE TRUE TRUE FALSE db1$olditems[db1$olditems==''] #wait, only one item is returned? [1] Levels: nuts soup db1[db1$olditems=='',] #somehow this works! olditems