これは、1 年前の私の回答よりも簡単に思える別のアプローチです。
単一のベクトルを分割します。
a1 <- c('1234567891234567891234567891234567891234')
a2 <- read.fwf(textConnection(a1), widths=rep(1, nchar(a1)), colClasses = 'numeric', header=FALSE)
a2
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22 V23 V24 V25 V26 V27 V28 V29 V30 V31 V32 V33 V34 V35 V36 V37 V38 V39 V40
1 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4
次の 3 つの等しい長さの長い数値を含むファイルを読み取ります。
# 1234567891234567891234567891234567891234
# 1888678912345678912345678912345678912388
# 1234999891234567891234567891234567891239
a1 <- read.table("c:/users/mmiller21/simple R programs/three_long_numbers.txt", colClasses = 'character', header = FALSE)
a2 <- read.fwf("c:/users/mmiller21/simple R programs/three_long_numbers.txt", widths=rep(1, max(nchar(a1$V1))), colClasses = 'numeric', header=FALSE)
a2
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22 V23 V24 V25 V26 V27 V28 V29 V30 V31 V32 V33 V34 V35 V36 V37 V38 V39 V40
1 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4
2 1 8 8 8 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 8 8
3 1 2 3 4 9 9 9 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 9
次の 3 つの長さが等しくない長い数値を含むファイルを読み取ります。
# 1234567891234567891234567891234567891234
# 188867891234567891234567891234567891238
# 12349998912345678912345678912345678912
a1 <- read.table("c:/users/mmiller21/simple R programs/three_long_numbersb.txt", colClasses = 'character', header = FALSE)
a2 <- read.fwf("c:/users/mmiller21/simple R programs/three_long_numbersb.txt", widths=rep(1, max(nchar(a1$V1))), colClasses = 'numeric', header=FALSE)
a2
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22 V23 V24 V25 V26 V27 V28 V29 V30 V31 V32 V33 V34 V35 V36 V37 V38 V39 V40
1 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4
2 1 8 8 8 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 8 NA
3 1 2 3 4 9 9 9 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 NA NA
複数の列を含むデータ ファイル内の長い数値の 1 つの列を分割するコードを次に示します。この例では、列 2 の各数値は同じ長さです。
# -10 1234567891234567891234567891234567891234 -100
# -20 1888678912345678912345678912345678912388 -200
# -30 1234999891234567891234567891234567891239 -300
a1 <- read.table("c:/users/mark w miller/simple R programs/three_long_numbers_Oct25_2013.txt", colClasses = c('numeric', 'character', 'numeric'), header = FALSE)
a2 <- read.fwf(textConnection(a1$V2), widths=rep(1, nchar(a1$V2)[1]), colClasses = 'numeric', header=FALSE)
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22 V23 V24 V25 V26 V27 V28 V29 V30 V31 V32 V33 V34 V35 V36 V37 V38 V39 V40
1 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4
2 1 8 8 8 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 8 8
3 1 2 3 4 9 9 9 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 9