[R] read.table (again)

Jim Lemon jim at bitwrit.com.au
Wed Nov 4 11:48:38 CET 2009


On 11/04/2009 09:03 PM, Sybille Wendel (Udata) wrote:
> ...
> That is, that every 4 digits there is a new number, but when the 
> number is > 999, R thinks of course that the number consists of more 
> than 4 digits. So, R can't read in the table.
> Is there a way I can tell R, that every 4 digits, a new number begins?
> I treif different things with "sep".
> data <- read.table ("RA19930101.txt",header=F,sep=)
Hi Sybille,
It looks to me as though you have a ragged matrix of values with a 3 
character field and then a varying number of 4 character fields. Here is 
a rather messy pair of functions that I think do what you want.

split34<-function(x) {
  # split the input string into single characters
  xsplit<-strsplit(x,"")
  lenx<-length(xsplit[[1]])
  newx<-paste(xsplit[[1]][1:3],sep="",collapse="")
  for(val in seq(4,lenx,by=4))
   newx<-c(newx,paste(xsplit[[1]][val:(val+3)],sep="",collapse=""))
  return(newx)
}

read.dat34<-function(file=NA) {
  input_strings<-scan(file,"",sep="\n")
  datalist<-vector("list",length(input_strings))
  for(elem in 1:length(input_strings))
   datalist[[elem]]<-split34(input_strings[elem])
  return(datalist)
}

read.dat34("RA19930101.txt")

Jim




More information about the R-help mailing list