[R] How to read a file including strings and numbers.

Jinsong Zhao jszhao at yeah.net
Sat Jun 1 10:09:22 CEST 2013


On 2013/6/1 15:54, Jie Tang wrote:
> hi R-users,
>    I have a file as shown below,and the first column is time information.
>
> "2012-08-07 00:00:00",4174830,5,8.1,34.5,9.5,32,14
> "2012-08-07 00:00:01",4174831,4.7,8.6,34.5,9.9,29,14
> "2012-08-07 00:00:02",4174832,4.7,8.6,34.5,9.9,29,14
> "2012-08-07 00:00:03",4174833,5,8.5,34.5,9.8,30,14
> "2012-08-07 00:00:04",4174834,5,8.5,34.5,9.8,30,14
> "2012-08-07 00:00:05",4174835,5.4,8.3,34.6,9.9,33,14
> "2012-08-07 00:00:06",4174836,5.4,8.3,34.6,9.9,33,14
> "2012-08-07 00:00:07",4174837,5.1,7.8,34.5,9.3,33,14
> "2012-08-07 00:00:08",4174838,5.1,7.8,34.5,9.3,33,14
> "2012-08-07 00:00:09",4174839,5.3,7.8,34.5,9.4,34,14
> "2012-08-07 00:00:10",4174840,5.3,7.8,34.5,9.4,34,14
> "2012-08-07 00:00:11",4174841,5.4,8.3,34.5,9.9,33,14
> "2012-08-07 00:00:12",4174842,5.4,8.3,34.5,9.9,33,14
> "2012-08-07 00:00:13",4174843,5.6,8.3,34.5,10,34,14
> "2012-08-07 00:00:14",4174844,5.6,8.3,34.5,10,34,14
> "2012-08-07 00:00:15",4174845,5.5,8.1,34.5,9.8,34,14
> "2012-08-07 00:00:16",4174846,5.5,8.1,34.5,9.8,34,14
> "2012-08-07 00:00:17",4174847,4.3,7.4,34.4,8.6,30,14
>
> I want to read them into a array by such command
>
> wnd_data<-scan("obs.dat",sep=",",what=list(TIME="",rec=0,U=0,V=0,T=0,WS=0,WD=0,ST=0,TA=0,RH=0),na.strings
> = "NAN")

wnd_data <- read.table("obs.dat", header = FALSE, sep = ",")
colnames(wnd_data) <- c("TIME","rec","U","V","T","WS","WD","ST","TA","RH")

HIH...
>
> but R could not excute sucefully and says that :
> error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,
>   :
>    scan() require 'a real', but not "2012-08-0700:00:01"'
>
> How could I resolve this problem ?
> Thanks.
>



More information about the R-help mailing list