2015-05-22 90 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.