[R] read.table() question

Duncan Murdoch murdoch.duncan at gmail.com
Wed Oct 19 23:07:11 CEST 2016


On 19/10/2016 4:54 PM, Rich Shepard wrote:
>    The file, daily_records.dat, contains these data:
>
> "station","date","amount"
> "0.3E",2014-01-01,
> "0.3E",2014-01-02,
> "0.3E",2014-01-03,0.01
> "0.3E",2014-01-04,0.00
> "0.3E",2014-01-05,0.00
> "0.3E",2014-01-06,0.00
> "0.3E",2014-01-07,0.10
> "0.3E",2014-01-08,0.22
> "0.3E",2014-01-09,0.49
>
>    Using read.table("daily_records.dat", header = TRUE, sep = ",", quote =
> "\"\"") the data are assigned to a data.frame named 'rain.'
>
>    I expect the structure to show station and date as factors with amount as
> numeric, but they're all factors:
>
> str(rain)
> 'data.frame':	341 obs. of  3 variables:
>   $ station: Factor w/ 6 levels "0.3E","0.6W",..: 1 1 ...
>   $ date   : Factor w/ 62 levels "2013-12-01","2013-12-02",..: 32 33 34 ...
>   $ amount : Factor w/ 48 levels "","0.00","0.01",..: 1 1 3 2 ...
>
>    Why is amount taken as a factor rather than numeric? I do not recall
> having numbers read as factors before this.

I don't get that from those 9 observations, so there's likely something 
else going on further down in the file.

Try which(is.na(as.numeric(as.character(rain$station)))) to find out 
which lines are causing problems for that column, and similarly for 
rain$amount.

Duncan Murdoch


>
>    I expect to need to convert dates using as.Date() but not to convert
> numbers.
>
> TIA,
>
> Rich
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>



More information about the R-help mailing list