2015-05-22 91 views
0

我是R的新手,并试图学习如何阅读下面的文本。我使用R - 从一个文本文件读取几个数据表

data <- read.table("myvertices.txt", stringsAsFactors=TRUE, sep=",")

希望传达的“FID ......”应该用逗号分隔的相关下面这些数字。 我得到的错误是:

在扫描(文件,什么,n最大,九月,十二月,报价,跳过,nlines,na.strings,错误: 线13没有2元

我将如何读下面的格式

FID001: 
-120.9633,51.8496 
-121.42749,52.293 
-121.25453,52.3195 
FID002: 
-65.4794,47.69011 
-65.4797,47.0401 
FID003: 
-65.849,47.5215 
-65.467,47.515 

成类似

FID001 -120.9633 51.8496 
FID001 -121.42749 52.293 
FID001 -121.25453 52.3195 
FID002 -65.4794 47.69011 
FID002 -65.4797 47.0401 
FID003 -65.849 47.5215 
FID003 -65.467 47.515 

回答

1

以下是实现此目的的一种可能方法:

data <- read.table("myvertices.txt")   # Read as-is. 
fid1 <- c(grep("^FID", data$V1), nrow(data) +1) # Get the row numbers containing "FID.." 
df1 <- diff(x = fid1, lag = 1)     # Calculate the length+1 rows to read 
listdata <- lapply(seq_along(df1), 
        function(n) cbind(FID = data$V1[fid1[n]], 
            read.table("myvertices.txt", 
               skip = fid1[n], 
               nrows = df1[n] -1, 
               sep = ","))) 
data2 <- do.call(rbind, listdata) # Combine all the read tables into a single data frame.