2015-02-10 74 views
1

我有一個統計問題,我想用R來解決。假設我有2個指數,Index1描述隨時間推移的平均價格水平,Index2描述了隨時間的平均租金水平。計算R中的平均指數與指數的比率

這是我的數據(幀):

Year Index1 Index2 
1995 100  77.0033 
1996 106.63 79.3342 
1997 110.45 81.8608 
1998 114.4 84.0633 
1999 121.75 86.1133 
2000 130.59 88.7758 
2001 148.85 91.4483 
2002 161.43 93.9042 
2003 179.39 95.57 
2004 204.59 97.1075 
2005 227.58 99.9995 
2006 253.17 102.2792 
2007 277.45 104.0525 
2008 276.42 107.1633 
2009 261.26 109.8667 
2010 280.81 111.9058 
2011 295.91 114.0408 
2012 306.63 115.56 
2013 NA  117.2691 
2014 NA  118.2967 

編輯:我想計算的價格與租金的平均,換句話說的Index1 /索引2的長時間平均比。之後我想計算平均值的百分比差異(每年)。我怎樣才能做到這一點?

最好的問候, 吉爾斯

編輯:這裏是dput(DF)

structure(list(Year = c(1995, 1996, 1997, 1998, 1999, 2000, 2001, 
2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 
2013, 2014), Price = c("100", "106.63", "110.45", "114.4", "121.75", 
"130.59", "148.85", "161.43", "179.39", "204.59", "227.58", "253.17", 
"277.45", "276.42", "261.26", "280.81", "295.91", "306.63", "NA", 
"NA"), Rent = c(77.0033, 79.3342, 81.8608, 84.0633, 86.1133, 
88.7758, 91.4483, 93.9042, 95.57, 97.1075, 99.9995, 102.2792, 
104.0525, 107.1633, 109.8667, 111.9058, 114.0408, 115.56, 117.2691, 
118.2967)), .Names = c("Year", "Price", "Rent"), row.names = c(NA, 
-20L), class = "data.frame") 
+0

如果你想與索引2新科拉姆從100開始,你可以做df ['index2_base100'] < - df $ index2/df $ index2 [1] * 100 – agenis 2015-02-10 15:35:02

+0

@agenis哇!感謝您的提示。我總是這樣做更復雜:) 但是,如果有NA值呢? – 2015-02-10 15:36:38

+0

「長時間」的比例是什麼意思?如果您發佈預期結果以更好地理解 – agenis 2015-02-10 16:04:42

回答

1

鑑於您在發佈註釋所需的輸出,我可以建議這樣的代碼:

library(ggplot2) 
df    <- data.frame(apply(df, 2, as.numeric)) 
df['Rent_b100'] <- df$Rent/df$Rent[1]*100 
df['ratio']  <- with(df, Price/Rent_b100) 
average_ratio <- mean(df$ratio, na.rm=T) 

ggplot(data=df) + 
    geom_line(aes(x=Year, y=ratio), color="blue", size=2) + 
    geom_hline(yintercept=average_ratio, color="purple",size=2) + 
    geom_text(data=data.frame(y=c(2, 1.2), x=mean(df$Year), label=c("rent", "buy")), 
      aes(x=x, y=y, label=label), size=8) + 
    geom_text(aes(x=df$Year[1], y=average_ratio*1.05, label=round(average_ratio, 2)), color="purple") 

這樣做具有以下圖表: enter image description here

+0

I印象深刻!謝謝:) – 2015-02-10 17:17:46

+0

但是如果我想使用.xlsx文件中的「我的」數據,我仍然存在NA強制的問題 – 2015-02-11 09:00:45

+0

我編輯了我的代碼,以便您可以使用您粘貼'dput'的數據。我只收到警告消息,但沒有錯誤或額外的NAs。 – agenis 2015-02-11 12:34:47

1

如果我明白你的意思,你首先要平均Index1/Index2,即(假設你的數據幀是df ):

average = mean(df$Index1/df$Index2, na.rm = TRUE) 

然後在數據框中添加一列以顯示年度變化(例如,增加值爲正數) :

df$variation = df$Index1/df$Index2/average - 1 
+0

我用R得到平均值1.983,但用excel得到1.627785。如果我檢查,R值對應於中值。這個錯誤我:/ – 2015-02-10 16:09:21

+0

我也收到一個錯誤消息,計算平均值:'在df $ Index1/df $ Index2中的錯誤:非二進制運算符的非數值參數' – 2015-02-10 16:27:58

+0

Your Price is not numeric;嘗試平均之前'df $ Price = as.numeric(df $ Price)' – clemlaflemme 2015-02-10 16:32:24