2012-04-10 7 views
1

名前の配列があり、データフレームの列名にこれらの名前を使用したいが、いくつかのエラーが発生している。私はどのようにこれを行うには正確ではないが、これは私がこれまで持っているものです。データフレーム列名から配列値

windspeeds = data.frame() 
cities <- c("albuquerque_nm", "boston_ma", "charlotte_nc", "dallas_tx", "denver_co", "helena_mt", "louisville_ky", "pittsburgh_pa", "salt_lake_city_ut", "seattle_wa") 
for(i in 1:10){ 
    fastest <- read.delim(paste("http://www.itl.nist.gov/div898/winds/data/nondirectional/datasets/", cities[i], ".prn", sep=""), col.names=c("NULL", "fastest", "NULL", "NULL"), skip=4, header=F, sep="")$fastest 
    windspeeds$cities[i] = fastest 
} 

私はこのエラーを取得しています:

Error in `$<-.data.frame`(`*tmp*`, "cities", value = 59L) : 
replacement has 1 rows, data has 0 
In addition: Warning message: 
In windspeeds$cities[i] = fastest : 
number of items to replace is not a multiple of replacement length 

は、私は、文字列または定数のいくつかのタイプに配列を変換する必要がありますか?

+0

そこにあなたのループのオブジェクト 'data'はありますか?ああ、あなたはそれを更新しました... – Ben

答えて

3

あなたの問題の1つは、クエリが都市ごとに同じ数のレコードを返さないことです(免責事項、私はあなたのデータやその外観を知りません)。

x <- lapply(cities, function(x) 
    read.delim(paste("http://www.itl.nist.gov/div898/winds/data/nondirectional/datasets/", x, ".prn", sep=""), 
      col.names=c("NULL", "fastest", "NULL", "NULL"), skip=4, header=FALSE, sep="")$fastest 
      ) 

は、Xは、今のようになります:

> str(x) 
List of 10 
$ : int [1:46] 59 49 51 52 57 52 45 54 49 64 ... 
$ : int [1:42] 50 55 79 56 53 41 51 51 65 62 ... 
$ : int [1:29] 33 42 40 52 42 48 52 51 46 51 ... 
$ : int [1:32] 48 45 46 45 43 53 43 58 46 43 ... 
$ : int [1:33] 42 51 49 44 47 47 50 44 44 54 ... 
$ : int [1:48] 58 58 58 58 70 55 56 62 59 70 ... 
$ : int [1:39] 40 39 50 53 50 51 54 54 51 50 ... 
$ : int [1:18] 47 56 60 44 54 50 42 52 47 47 ... 
$ : int [1:46] 53 49 40 53 55 40 49 46 61 41 ... 
$ : int [1:10] 38 44 35 46 42 45 41 45 42 43 

そして持っているにかかわらず、ここでは、おそらく物事を行うには、より「R-っぽい」方法ですリストオブジェクトにデータを読み取ることが一つの方法です記述統計:

> do.call(rbind, lapply(x, summary)) 
     Min. 1st Qu. Median Mean 3rd Qu. Max. 
[1,] 45 49.50 53.0 55.02 57.00 85 
[2,] 41 49.25 54.5 56.26 60.75 85 
[3,] 33 39.00 42.0 44.86 51.00 65 
[4,] 39 45.75 48.0 49.16 51.50 67 
[5,] 42 44.00 48.0 48.67 51.00 61 
[6,] 42 49.00 55.0 54.04 58.00 71 
[7,] 38 43.50 49.0 48.74 52.50 66 
[8,] 39 45.00 47.0 48.44 53.50 60 
[9,] 40 45.25 49.0 50.41 54.00 69 
[10,] 35 41.25 42.5 42.10 44.75 46 

あなたは各都市のレコード数が同じかどうかは不明であるが、うまくいけば、これは正しい道をあなたを取得します。

+0

チップのおかげで、私はRであまりにも多くのプログラミングをしていないし、慣習のいずれかを知らない。 – trev9065

+0

@ trev9065 - 心配しないで!誰もがある時点で学ばなければならない。 Rは他のプログラミング言語とはまったく異なっていますが、Rで考えることができれば、比較的少量のコードでどれくらい手に入るかには驚くでしょう! – Chase

+0

に行の名前を付けて都市と照合する方法はありますか? – trev9065

関連する問題