[R] Strange column shifting with read.table

David Winsemius dwinsemius at comcast.net
Mon Aug 3 05:43:27 CEST 2009


On Aug 2, 2009, at 10:46 PM, Rolf Turner wrote:

>
> On 3/08/2009, at 1:48 PM, David Winsemius wrote:
>
>>
>> On Aug 2, 2009, at 7:29 PM, Rolf Turner wrote:
>>
>>>
>>> On 3/08/2009, at 11:14 AM, David Winsemius wrote:
>>>
>>>>
>>>> On Aug 2, 2009, at 7:02 PM, Noah Silverman wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> It seems as if the problem was caused by an odd quirk of the  
>>>>> "scale"
>>>>> function.
>>>>>
>>>>> Some of my data have NA entries.
>>>>>
>>>>> So, I substitute 0 for any NA with:
>>>>> rawdata[is.na(rawdata)] <- 0
>>>>
>>>> Perhaps this would have done what you intended:
>>>>
>>>> rawdata[is.na(rawdata), ] <- 0
>>>
>>> 	I don't think this works at all.  E.g.:
>>>
>>> 	set.seed(42)
>>> 	junk <- matrix(rnorm(60),12,5)
>>> 	junk[sample(1:60,14)] <- NA
>>> 	junk[is.na(junk),] <- 0 # Throws an error.
>>> 	junk[is.na(junk)] <- 0  # Gives the desired result.\\
>>
>> Then how about? ....
>>
>> junk2 <- replace(junk, is.na(junk), 0)
>
> That certainly works --- and is a potentially useful piece of syntax  
> of which I was
> previously unaware (thank you) --- but the OP's syntax also worked  
> (and gives identical results
> to yours).  I.e. the OP's problem was *not* induced by the way he  
> went about replacing
> NA's by zeroes, irrespective of the fact that that's a silly thing  
> to do.

Go back to the original posting. The OP was complaining about the fact  
that rawdata[is.na(rawdata)] <- 0 threw an error before he ran that R  
object through scale. He did not realize that rawdata was a dataframe  
in the first instance and a matrix in the second. The syntax of the  
"[" operator  would not support the construction that he and you were  
using when it was applied to a dataframe. The code I offered does not  
seem to suffer from that deficiency.


>
> 	cheers,
>
> 		Rolf Turner


David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list