Data:-
Date Value
2010 Q1 x
2011 Q4 x
2010 Q1 x
2010 Q3 x
2015 Q2 x
2016 Q1 x
2015 Q1 x
2013 Q4 x
2012 Q3 x
我需要計算單個年份中的點數並最終繪製它們。所以我寫了一個代碼來完成這項工作,但是它涉及硬編碼循環範圍。計算R中值的實例
Code:-
require(zoo)
year=count=""
for (i in 2009:2016){
year=c(year,i+1)
count=c(count,length(data[which(data$Date>i & data$Date<i+1),1]))
}
year=year[2:length(year)]
count=count[2:length(count)]
plot(year,count)
Output:-
year "2010" "2011" 2012" "2013" "2015" "2016"
count "3" "1" "1" "1" "2""1"
我在尋找基本上兩件事的幫助。首先是我如何推廣代碼,其次是我可以替代連接方法,因爲我總是需要刪除代碼末尾的每個向量的第一個元素。謝謝。
'table(data $ Date)'給出了你的輸出結果,如果你只需要一年,我就不會真正理解你的數據結構。它看起來像你的示例數據中的日期是一個字符串(一年四分之一),但你把它作爲一個數字年只在循環中 – rawr
@rawr日期類'yearqtr',因此我試着比較它,它的工作。要使用表格方法,我需要首先分離出一年,然後使用它。它以任何方式工作。謝謝。 – Meesha
所以你使用動物園包?請注意你的問題中的任何額外的軟件包 – rawr