[R] Convert list to data frame while controlling column types
Alexander Shenkin
ashenkin at ufl.edu
Sun Aug 23 08:47:01 CEST 2009
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.
> 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) })
More information about the R-help
mailing list