2014-03-06 35 views
2

逐年ATAN2函數我有創建的人造數據集:迴路的中的R

x<-rnorm(100,10,10) 
y<-rnorm(100,20,10) 
Location<-c((rep("AB", 40)),(rep("TA", 30)),(rep("OP", 30))) 
Year<-c((rep("1999", 10)),(rep("2000", 9)),(rep("2001", 12)),(rep("2002", 9)),(rep("1999", 7)),(rep("2000", 6)),(rep("2001", 6)),(rep("2002", 11)),(rep("1999", 12)),(rep("2000", 8)),(rep("2001", 5)),(rep("2002", 5))) 
Data<-cbind(x,y,Location,Year) 

> head(Data) 
     x     y     Location Year 
[1,] "1.8938661556415" "19.851256070398" "AB"  "1999" 
[2,] "21.0735971323312" "17.4993965352294" "AB"  "1999" 
[3,] "30.8347289164302" "7.63333686308105" "AB"  "1999" 
[4,] "8.913993138201" "14.7085296541221" "AB"  "1999" 
[5,] "20.8309225677419" "12.0888505284667" "AB"  "1999" 
[6,] "25.3978549194374" "20.47154776064" "AB"  "1999" 

我想利用各個x和這樣ÿ爲一體的arc2tan:

Theta<-atan2(y[i+1]-y[i],x[i+1]-x[i]) 

但我只希望在年內每年都能這樣做,這意味着我不想在1999年到2000年之間或2001年到2002年之間找到theta,只有在同一年的同一年的x和y點之間。

我原本寫了一個循環,做了上面的(我不想做的),我想知道是否有人知道如何改變它,這樣循環就會停止並重置每一年。原始迴路如下:

for (i in 1:length(x)-1){ 
    Theta[i]<-atan2(y[i+1]-y[i],x[i+1]-x[i]) 
} 

任何幫助?

回答

1

你可以試試這個。

# a smaller test data set 
x <- rnorm(24, 10, 10) 
y <- rnorm(24, 20, 10) 
loc <- rep(c("A", "B"), each = 4) 
year <- rep(1999:2001, each = 8) 
df <- data.frame(x, y, loc, year) 

df 

# apply function on subsets defined by location and year 
# use tail and head to 'lag' y and x 
by(df, df[ , c("loc", "year")], function(x){ 
with(x, atan2(y = tail(y, - 1) - head(y, -1), x = tail(x, -1) - head(x, - 1))) 
}) 

# loc: A 
# year: 1999 
# [1] 2.306794 -2.363359 1.065151 
# --------------------------------------------------------------------------- 
# loc: B 
# year: 1999 
# [1] -1.077345 1.161944 -2.101823 
# --------------------------------------------------------------------------- 
# loc: A 
# year: 2000 
# [1] -1.76557207 1.79463661 -0.05251002 
# --------------------------------------------------------------------------- 
# loc: B 
# year: 2000 
# [1] 2.753115 -1.468055 -1.624389 
# ...snip... 

A dplyr替代方案。由於每個組中功能的結果長度不等於組大小或1,因此dplyr根本不滿意數據幀(請參閱herehere)。解決方法是爲dplyr提供data.table。當然,這裏只有一個data.table解決方案將是最清潔的。我留給那些比我更熟悉data.table的人......

library(data.table) 
library(dplyr) 
dt <- data.table(df) 
dt %.% 
    group_by(loc, year) %.% 
    mutate(
    atan = atan2(lead(y, default = NULL) - lag(y, default = NULL), 
      lead(x, default = NULL) - lag(x, default = NULL))) 

#   x   y loc year  atan 
# 1 19.826573 18.354265 A 1999 2.30679446 
# 2 11.856696 27.153197 A 1999 -2.36335869 
# 3 -3.362242 12.150775 A 1999 1.06515149 
# 4 11.126841 38.320662 A 1999 2.30679446 
# 5 12.616396 31.782969 A 2000 -1.76557207 
# 6 8.492305 10.877870 A 2000 1.79463661 
# 7 4.921766 26.561845 A 2000 -0.05251002 
# 8 14.398730 26.063752 A 2000 -1.76557207 
# 9 11.800173 30.215422 A 2001 -2.74907150 
# 10 -6.473259 22.650127 A 2001 0.11997030 
# 11 6.528055 24.217425 A 2001 -1.71122202 
# 12 4.951238 13.062497 A 2001 -2.74907150 
# 13 1.640049 19.886848 B 1999 -1.07734532 
# 14 4.123603 15.269110 B 1999 1.16194418 
# 15 14.548780 39.330885 B 1999 -2.10182331 
# 16 6.925468 26.350556 B 1999 -1.07734532 
# ...snip... 
+0

太棒了!我非常感謝你的幫助。不幸的是,我不能使用第二個建議,因爲我嘗試加載包'dplyr'時出錯。你有建議如何將第一個建議的結果變成向量或列表形式嗎? – KL2014

+1

很高興聽到您發現我的答案有用。加載'dplyr'時出現了哪個錯誤?我假設你已經安裝了它......;)'by'的輸出是一個'list'(在R術語中)。您可以使用'unlist'將其簡化爲一個向量。 – Henrik

+0

哈哈哈,我知道我是一名業餘R用戶,但我知道我需要先安裝它。嘗試安裝時出現錯誤。有錯誤說明:警告消息: 在utils ::: getDependencies(pkgs,available = avail): 包'dplyr'不可用(對於R版本2.14.1)。 – KL2014