[R] Convert list to data frame while controlling column types
David Winsemius
dwinsemius at comcast.net
Sun Aug 23 16:58:27 CEST 2009
On Aug 23, 2009, at 2:47 AM, Alexander Shenkin wrote:
> On 8/21/2009 3:04 PM, David Winsemius wrote:
>>
>> On Aug 21, 2009, at 3:41 PM, Alexander Shenkin wrote:
>>
>>> Thanks everyone for their replies, both on- and off-list. I should
>>> clarify, since I left out some important information. My original
>>> dataframe has some numeric columns, which get changed to character
>>> by
>>> gsub when I replace spaces with NAs.
>>
>> If you used is.na() <- that would not happen to a true _numeric_
>> vector
>> (but, of course, a numeric vector in a data.frame could not have
>> spaces,
>> so you are probably not using precise terminology).
>
> I do have true numeric columns, but I loop through my entire dataframe
> looking for blanks and spaces for convenience.
I still have problems with this statement. As I understand R, this
should be impossible. I have looked at both you postings and neither
of them clarify the issues. How can you have blanks or spaces in an R
numeric vector?
>
>> You would be well
>> advised to include the actual code rather than applying loose
>> terminology subject you your and our misinterpretation.
>
> I did include code in my previous email. Perhaps you were looking for
> different parts.
>
>>
>> ?is.na
>>
>>
>> I am guessing that you were using read.table() on the original
>> data, in
>> which case you should look at the colClasses parameter.
>>
>
> yep - I use read.csv, and I do use colClasses. But as I mentioned
> earlier, gsub converts those columns to characters. Thanks for the
> tip
> about is.na() <-. I'm now using the following, thus side-stepping the
> whole "controlling as.data.frame's column conversion" issue:
>
> final_dataf = lapply(final_dataf, function(x){ is.na(x) <-
> + grep('^\\s*$',x); return(x) })
Good that you have a solution.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list