2016-11-15 10 views
0

私はまだかなり新しいRであり、特定の方法でデータを要約しようとしています。ここでそれを説明するために、私はnasaweatherパッケージから天気データを使用しています。一例として、特定の日の平均気温を取得し、このデータセットに含まれる3つの起源と12の月間に表示したいと考えています。クエリでデータセット内の選択されたエントリを要約する

私は興味のある日を指定して、空のデータフレームを作成してから、月を使ってforループを実行して平均を計算します月ごとに温度を計算し、それらをデータフレームにバインドします。最後に、私は、列名を調整し、結果をプリントアウト:私の手で

library(nasaweather) 
library(magrittr) 
library(dplyr) 

query_day = 15 
data_output <- data.frame(month = numeric(), 
       EWR = numeric(), 
       JFK = numeric(), 
       LGA = numeric()) 

for (i in 1:12) { 
    data_subset <- weather %>% 
    filter(day == query_day, month == i) %>% 
    summarize(
     EWR = mean(temp[origin == "EWR"]), 
     JFK = mean(temp[origin == "JFK"]), 
     LGA = mean(temp[origin == "LGA"])) 
    data_output <- rbind(data_output, cbind(i, data_subset)) 
    rm(data_subset) 
} 

names(data_output) <- c("month", "EWR", "JFK", "LGA") 
print(data_output) 

をこれには、以下が得られます。

month  EWR  JFK  LGA 
1  1 39.3725 39.0875 38.9150 
2  2 42.1625 39.3425 42.9050 
3  3 37.4150 36.7775 37.3025 
4  4 50.1275 48.1550 49.2050 
5  5 58.8725 55.7150 59.1575 
6  6 70.7825 70.2950 71.5700 
7  7 86.9900 85.1225 87.2000 
8  8 69.2075 69.0725 69.9425 
9  9 60.6350 61.2125 61.7375 
10 10 59.8850 58.3850 60.5150 
11 11 45.7475 45.1700 49.0700 
12 12 32.4950 38.0975 34.0325 

を正確に私が欲しいものです。私のコードははるかに複雑であるように思えて、この仕事を簡単にする方法があるかどうか尋ねたいと思っています。

+1

あなただけの集計関数を使用し、 '<再構築することができます - 集約(一時を〜月+起源、天気、平均); reshape(a、id = 'month'、...) ' –

+0

@Dirkに感謝しますが、私が正しいとすれば、特定の日の平均気温ではなく月全体の平均気温が得られます。集計関数内でそれを指定する方法はありますか? –

+0

ああ、ああ、それを逃した - 集計(一時的な月+起源、天気[天気$日==クエリ_日、]、平均); (a、id = '月'、...) ' –

答えて

1

コードにはさまざまな問題がありますが、主なものはgroup_byを最初にしなかったことです。それを含めるとすぐに、これは簡単にうんざりになります。最初にあなたのコードに私のコメントを見て、そして一番下に、その後確定したコード:

library(nasaweather) ## Wrong package 
# library(magrittr) ## not needed, it's called by dplyr 
library(dplyr) 

query_day = 15 
# data_output <- data.frame(month = numeric(), ## We won't need to specify this explicitly 
## (but you are right that you should specify this in a for loop. Go one step 
## further by actually telling the data.frame how many rows to expect. 
## But not in this case cause we won't use for loop) 
         # EWR = numeric(), 
         # JFK = numeric(), 
         # LGA = numeric()) 

for (i in 1:12) { ## You don't need to do a for loop... you can do it with the summarize_by function. 
    data_subset <- weather %>% 
    filter(day == query_day, month == i) %>% 
    summarize(  ## Before doing summarize, you need a group_by to say what to summarize_by 
     EWR = mean(temp[origin == "EWR"]), 
     JFK = mean(temp[origin == "JFK"]), 
     LGA = mean(temp[origin == "LGA"])) 
    data_output <- rbind(data_output, cbind(i, data_subset)) ## If you're doing the group_by, this step isn't required. 
    # rm(data_subset) ## You don't have to remove temporary datasets... 
## When the for loop ends, they are automatically removed. 
} 

names(data_output) <- c("month", "EWR", "JFK", "LGA") 
print(data_output) 

################### Better code: 
library(nycflights13) ## your the package you waant is nycflights13... not nasaweather 
library(dplyr) 

query_day = 15 

weather %>% 
    filter(day == query_day) %>% 
    group_by(month) %>% 
    summarize(
     EWR = mean(temp[origin == "EWR"]), 
     JFK = mean(temp[origin == "JFK"]), 
     LGA = mean(temp[origin == "LGA"])) -> data_output 

data_output 

収量:

> data_output 
# A tibble: 12 × 4 
    month  EWR  JFK  LGA 
    <dbl> <dbl> <dbl> <dbl> 
1  1 39.3725 39.0875 38.9150 
2  2 42.1625 39.3425 42.9050 
3  3 37.4150 36.7775 37.3025 
4  4 50.1275 48.1550 49.2050 
5  5 58.8725 55.7150 59.1575 
6  6 70.7825 70.2950 71.5700 
7  7 86.9900 85.1225 87.2000 
8  8 69.2075 69.0725 69.9425 
9  9 60.6350 61.2125 61.7375 
10 10 59.8850 58.3850 60.5150 
11 11 45.7475 45.1700 49.0700 
12 12 32.4950 38.0975 34.0325 
+0

ありがとうございます@これらすべての有益なコメントのために、多くのことを学びましょう!私は最初に "group_by"で試してみましたが、うまく動作しませんでした。しかし、改善されたバージョン(## Better code :)の11行を実行すると、 '' EWR JFK LGAのためにそれぞれ1つの値を持つ '1 54.47438 53.86937 55.12938'と12 x 4 tibbleが返されます。私は12ヶ月間の平均だと思う。私が(ここで)何が間違っているのか? –

+0

奇妙な音...コンソールをクリアするか、RStudioを再起動してもう一度やり直してください。私はちょうど再試行し、正しく動作します。 –

+0

RStudioを再起動すると仕事が終了しました。 –

関連する問題