我有一個樣本數據集像這樣高效地讀取數據:與R中的多個分離線
8 02-Model (Minimum)
250.04167175293 17.4996566772461
250.08332824707 17.5000038146973
250.125 17.5008907318115
250.16667175293 17.5011672973633
250.20832824707 17.5013771057129
250.25 17.502140045166
250.29167175293 17.5025615692139
250.33332824707 17.5016822814941
7 03 (Maximum)
250.04167175293 17.5020561218262
250.08332824707 17.501148223877
250.125 17.501127243042
250.16667175293 17.5
250.20832824707 17.5016021728516
250.25 17.5024681091309
250.29167175293 17.5043239593506
上的數據文件的第一列單元的行的該特定數據(即,對於02-模型數(最小))。然後,在8行後,我有另一行7 03 (Maximum)
這意味着03(最大)我將有7行數據。
我寫功能是如下:
readts <- function(x)
{
path <- x
# Read the first line of the file
hello1 <- read.table(path, header = F, nrows = 1,sep="\t")
tmp1 <- hello1$V1
# Read the data below first line
hello2 <- read.table(path, header = F, nrows = (tmp1), skip = 1,
col.names = c("Time", "value"))
hello2$name <- c(as.character(hello1$V2))
# Read data for the second chunk
hello3 <- read.table(path, header = F, skip = (tmp1 + 1),
nrows = 1,sep="\t")
tmp2 <- hello3$V1
hello4 <- read.table(path, header = F, skip = (tmp1 + 2),
col.names = c("Time", "value"),nrows=tmp2)
hello4$name <- c(as.character(hello3$V2))
# Combine data to create a dataframe
df <- rbind(hello2, hello4)
return(df)
}
我得到的輸出如下所示:
> readts("jdtrial.txt")
Time value name
1 250.0417 17.49966 02-Model (Minimum)
2 250.0833 17.50000 02-Model (Minimum)
3 250.1250 17.50089 02-Model (Minimum)
4 250.1667 17.50117 02-Model (Minimum)
5 250.2083 17.50138 02-Model (Minimum)
6 250.2500 17.50214 02-Model (Minimum)
7 250.2917 17.50256 02-Model (Minimum)
8 250.3333 17.50168 02-Model (Minimum)
9 250.0417 17.50206 03 (Maximum)
10 250.0833 17.50115 03 (Maximum)
11 250.1250 17.50113 03 (Maximum)
12 250.1667 17.50124 03 (Maximum)
13 250.2083 17.50160 03 (Maximum)
14 250.2500 17.50247 03 (Maximum)
15 250.2917 17.50432 03 (Maximum)
jdtrial.txt是我在上面示出的數據。但是,當我有多個分隔符的大數據時,我的函數不起作用,我需要添加更多的行,這使得函數更加混亂。有沒有更簡單的方法來讀取這樣的數據文件?謝謝。
預期的數據是我得到的數據。您可以嘗試的數據如下:
8 02-Model (Minimum)
250.04167175293 17.4996566772461
250.08332824707 17.5000038146973
250.125 17.5008907318115
250.16667175293 17.5011672973633
250.20832824707 17.5013771057129
250.25 17.502140045166
250.29167175293 17.5025615692139
250.33332824707 17.5016822814941
7 03 (Maximum)
250.04167175293 17.5020561218262
250.08332824707 17.501148223877
250.125 17.501127243042
250.16667175293 17.5
250.20832824707 17.5016021728516
250.25 17.5024681091309
250.29167175293 17.5043239593506
8 04-Model (Maximum)
250.04167175293 17.5020561218262
250.08332824707 17.501148223877
250.125 17.501127243042
250.16667175293 17.5
250.20832824707 17.5016021728516
250.25 17.5024681091309
250.29167175293 17.5043239593506
250.33332824707 17.5055828094482
不錯。這是迄今爲止最好的選擇。每個人都忽視了'fill = TRUE'的說法。 – thelatemail
它似乎簡短而方便,但我不熟悉動物園包。儘管你的解釋很有幫助。 –