2017-09-02 4 views
0

最終的に最終的なグラフを得るために私が作っている小さなプログラムです。私は2つの別々のデータセットを持っています。 1つはT0と呼ばれ、2つ目は私が持っているすべてのデータを含んでいます。私はこのプログラムが最初のデータフレームからT0値を取得し、その後T0年の3年前と3年後の最高価格を検索します。私のプログラムでT0を定義する

本質的に私のプログラムは、私が任意に選んだT0値を割り当てようとしています。それから私のデータベースで、t0年を除く毎年の最高価格を自動的に検索します。

私が直面している問題は、スケジュール内のT0値の実装です。私のコードを実行すると、それは正しく出てこない。

問題は明らかにT0を定義する方法と関係があります。 forループを使うべきですか?または私は行方不明の小さな微調整ですか?

最終結果が欲しかった:

Final result wanted

データベース例:

Data Base example

T0data:

structure(list(Company = structure(1:3, .Label = c("Amazon", 
"Cisco", "McDonald's"), class = "factor"), Year = c(2011L, 2008L, 
2013L), Price = c(182, 21.82, 95.15)), .Names = c("Company", 
"Year", "Price"), row.names = c(NA, 3L), class = "data.frame") 

すべてのデータ:

structure(list(Company = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L 
), .Label = c("Amazon", "Cisco", "McDonald's"), class = "factor"), 
    Year = c(2008L, 2008L, 2008L, 2008L, 2009L, 2009L, 2010L, 
    2010L, 2010L, 2011L, 2011L, 2012L, 2012L, 2013L, 2013L, 2014L, 
    2014L, 2014L, 2008L, 2010L, 2010L, 2010L, 2011L, 2011L, 2012L, 
    2012L, 2013L, 2013L, 2014L, 2014L, 2014L, 2015L, 2015L, 2016L, 
    2016L, 2016L, 2005L, 2005L, 2005L, 2006L, 2006L, 2007L, 2007L, 
    2007L, 2008L, 2008L, 2009L, 2009L, 2009L, 2010L, 2010L, 2011L, 
    2011L, 2011L), Price = c(91L, 77L, 81L, 87L, 63L, 88L, 110L, 
    75L, 117L, 170L, 190L, 215L, 245L, 316L, 275L, 330L, 378L, 
    390L, 55L, 62L, 66L, 65L, 72L, 98L, 93L, 88L, 99L, 101L, 
    94L, 103L, 96L, 99L, 116L, 112L, 123L, 113L, 19L, 17L, 18L, 
    20L, 19L, 26L, 31L, 27L, 24L, 21L, 14L, 22L, 18L, 26L, 22L, 
    14L, 16L, 15L)), .Names = c("Company", "Year", "Price"), class = "data.frame", row.names = c(NA, 
-54L)) 

マイコード:ここで

library(data.table) 
T0data<- read.csv(file = "C:/Users/My first file.csv", header = TRUE) 
Alldata<- read.csv(file = "C:/Users/My second file.csv", header = TRUE) 
d<-Alldata 
setDT(d) 
year_zero <- T0data$Year 
# Filter to include year_zero +/- 3 years and get Best result per company per year 
d <- d[Year >= year_zero - 3 & Yeae <= year_zero + 3, 
    .(Best_Result = max(Price, na.rm = TRUE)), by = .(Company, Year)] 
# Add T as interval to year_zero (and convert to factor in order to get all 
# values from 3 to 3 
d[, "T" := factor(Year - year_zero, levels = seq(-3, 3), ordered = TRUE)] 
# Cast to wide format (fill missing values with NA) 
dcast(d, Company ~T, value.var = "Best_Result", drop = FALSE) 
# Cast to wide format (fill missing values with "") 
dcast(d, Company~T, value.var = "Best_Result", drop = FALSE, fun.aggregate = paste0, 
    fill = "") 
+0

を!私の最初のファイル:https://drive.google.com/file/d/0B_m8D7TZHrwWd1Z3LUlFWEZJaE0/view?usp=sharing私の2番目のファイル:https://drive.google.com/open?id=0B_m8D7TZHrwWMTV4dng5akpEbWc –

+0

今すぐもっとよく見えますか? –

+0

完了!私の友人に耐えてくれてありがとう! –

答えて

0

ではなくdata.tableよりも、tidyverseからdplyr/tidyrパッケージを使用するソリューションだが、それは仕事をする必要がありますプロットを結果として生じる

library(dplyr); library(tidyr) 

T0.modified <- T0data %>% 

    # create year range based on each company's T0 year 
    mutate(Year.M1 = Year - 1, 
     Year.M2 = Year - 2, 
     Year.M3 = Year - 3, 
     Year.P1 = Year + 1, 
     Year.P2 = Year + 2, 
     Year.P3 = Year + 3) %>% 

    # convert to long format, match with Alldata based on both company & year 
    gather(reference.year, actual.year, -Company, -Price) %>% 
    left_join(Alldata, by = c("Company" = "Company", "actual.year" = "Year")) %>% 

    # keep T0 price for year T0, & use matched prices for all other years 
    mutate(Price = ifelse(reference.year == "Year", Price.x, Price.y)) %>% 

    # take maximum of all matched prices for each company each year 
    group_by(Company, reference.year) %>% 
    summarise(Price = max(Price)) %>% 
    ungroup() %>% 

    # order reference.year for correct sequence in ggplot's x-axis 
    mutate(reference.year = factor(reference.year, 
           levels = c("Year.M3", "Year.M2", "Year.M1", "Year", 
              "Year.P1", "Year.P2", "Year.P3"), 
          labels = c("T-3", "T-2", "T-1", "T0", "T+1", "T+2", "T+3"))) 

library(ggplot2) 

ggplot(T0.modified, 
     aes(x = reference.year, y = Price, group = Company, color = Company)) + 
    geom_line(aes()) + 
    xlab("Year") + theme_bw() 

plot

編集stat_summaryを使用して、各年の平均を追加:

完了
ggplot(T0.modified, 
     aes(x = reference.year, y = Price, group = Company, color = Company)) + 
    geom_line(aes()) + 
    xlab("Year") + theme_bw() + 

    stat_summary(fun.y = mean, geom = "line", group = 1, 
       linetype = 2, size = 1.5, colour = "grey") + 
    annotate("label", x = 7, y = 200, label = "Average", 
      fill = "grey", alpha = 0.5, hjust = 1) 

stat summary

+0

あなたは最高のZ.Linです!どうもありがとうございます! –

+0

ちょうど最後の質問ですが、単純に平均を線として表示する方法はありますか? –

+0

@SesoTheAstronautおそらく、具体的にしてください。平均何?同じT​​年のすべての企業の平均?各社の最大値ではなく平均値ですか? –

関連する問題