0

R: 多数の行からなるデータフレームがありますが、列は 1 つしかありません。各行には長い文字列があり、定期的に | で区切られています。マーク。| があるたびに文字を分割したい マークを付けて、列が多くなるようにします。

1995-01-01|33.399999999999999|40.299999999999997|35.399999999999999|35.0|37.200000000000003|23.399999999999999|23.199999999999999|47.399999999999999|49.200000000000003|49.200000000000003|48.100000000000001|42.299999999999997|58.200000000000003|17.399999999999999|50.700000000000003|5.2999999999999998|20.600000000000001|38.5|43.299999999999997 etc.

各文字列は日付で始まり、都市に対応する数字が続きます。変数名も 1 つの文字列としてリストされ、"." で区切る必要があります。マーク。

date.abilene_tx.akron_oh.albany_ny.albuquerque_nm.allentown_pa.amarillo_tx.anchorage_ak.asheville_nc.atlanta_ga etc.

どんな助けでも大歓迎です!

4

2 に答える 2

1

あなたのものに似ているかもしれない1列と10行のdata.frameは次のとおりです。

dat <- "1995-01-01|33.399999999999999|40.299999999999997|35.399999999999999|35.0|37.200000000000003|23.399999999999999|23.199999999999999|47.399999999999999|49.200000000000003|49.200000000000003|48.100000000000001|42.299999999999997|58.200000000000003|17.399999999999999|50.700000000000003|5.2999999999999998|20.600000000000001|38.5|43.299999999999997 "

df <- data.frame(col1 = rep(dat, 10))

Col1 の分割に基づく新しい列を持つ data.frame を次に示します。

foo <- data.frame(do.call('rbind', strsplit(as.character(df$col1),'|',fixed=TRUE)))
foo

           X1                 X2                 X3                 X4   X5                 X6
1  1995-01-01 33.399999999999999 40.299999999999997 35.399999999999999 35.0 37.200000000000003
2  1995-01-01 33.399999999999999 40.299999999999997 35.399999999999999 35.0 37.200000000000003
3  1995-01-01 33.399999999999999 40.299999999999997 35.399999999999999 35.0 37.200000000000003
4  1995-01-01 33.399999999999999 40.299999999999997 35.399999999999999 35.0 37.200000000000003
5  1995-01-01 33.399999999999999 40.299999999999997 35.399999999999999 35.0 37.200000000000003
6  1995-01-01 33.399999999999999 40.299999999999997 35.399999999999999 35.0 37.200000000000003
7  1995-01-01 33.399999999999999 40.299999999999997 35.399999999999999 35.0 37.200000000000003
8  1995-01-01 33.399999999999999 40.299999999999997 35.399999999999999 35.0 37.200000000000003
9  1995-01-01 33.399999999999999 40.299999999999997 35.399999999999999 35.0 37.200000000000003
10 1995-01-01 33.399999999999999 40.299999999999997 35.399999999999999 35.0 37.200000000000003
                   X7                 X8                 X9                X10                X11
1  23.399999999999999 23.199999999999999 47.399999999999999 49.200000000000003 49.200000000000003
2  23.399999999999999 23.199999999999999 47.399999999999999 49.200000000000003 49.200000000000003
3  23.399999999999999 23.199999999999999 47.399999999999999 49.200000000000003 49.200000000000003
4  23.399999999999999 23.199999999999999 47.399999999999999 49.200000000000003 49.200000000000003
5  23.399999999999999 23.199999999999999 47.399999999999999 49.200000000000003 49.200000000000003
6  23.399999999999999 23.199999999999999 47.399999999999999 49.200000000000003 49.200000000000003
7  23.399999999999999 23.199999999999999 47.399999999999999 49.200000000000003 49.200000000000003
8  23.399999999999999 23.199999999999999 47.399999999999999 49.200000000000003 49.200000000000003
9  23.399999999999999 23.199999999999999 47.399999999999999 49.200000000000003 49.200000000000003
10 23.399999999999999 23.199999999999999 47.399999999999999 49.200000000000003 49.200000000000003
                  X12                X13                X14                X15                X16
1  48.100000000000001 42.299999999999997 58.200000000000003 17.399999999999999 50.700000000000003
2  48.100000000000001 42.299999999999997 58.200000000000003 17.399999999999999 50.700000000000003
3  48.100000000000001 42.299999999999997 58.200000000000003 17.399999999999999 50.700000000000003
4  48.100000000000001 42.299999999999997 58.200000000000003 17.399999999999999 50.700000000000003
5  48.100000000000001 42.299999999999997 58.200000000000003 17.399999999999999 50.700000000000003
6  48.100000000000001 42.299999999999997 58.200000000000003 17.399999999999999 50.700000000000003
7  48.100000000000001 42.299999999999997 58.200000000000003 17.399999999999999 50.700000000000003
8  48.100000000000001 42.299999999999997 58.200000000000003 17.399999999999999 50.700000000000003
9  48.100000000000001 42.299999999999997 58.200000000000003 17.399999999999999 50.700000000000003
10 48.100000000000001 42.299999999999997 58.200000000000003 17.399999999999999 50.700000000000003
                  X17                X18  X19                 X20
1  5.2999999999999998 20.600000000000001 38.5 43.299999999999997 
2  5.2999999999999998 20.600000000000001 38.5 43.299999999999997 
3  5.2999999999999998 20.600000000000001 38.5 43.299999999999997 
4  5.2999999999999998 20.600000000000001 38.5 43.299999999999997 
5  5.2999999999999998 20.600000000000001 38.5 43.299999999999997 
6  5.2999999999999998 20.600000000000001 38.5 43.299999999999997 
7  5.2999999999999998 20.600000000000001 38.5 43.299999999999997 
8  5.2999999999999998 20.600000000000001 38.5 43.299999999999997 
9  5.2999999999999998 20.600000000000001 38.5 43.299999999999997 
10 5.2999999999999998 20.600000000000001 38.5 43.299999999999997
于 2013-12-05T02:31:00.507 に答える
1

次のコマンドを使用して、ファイルからデータをロードする必要があります。

 dat <- read.table(filename, sep="|")

これは、「|」で区切られた行を処理します しかし、「文字列」は「.」で区切られていると言うので、これらがhtatテキストファイルに何らかの形で混在している場合は、最初に入力で前処理を行う必要があるかもしれませんreadLines().

于 2013-12-05T17:02:54.463 に答える