2014-01-10 44 views
1

我遇到了一個與此question非常相似的問題,但是我的數據分爲兩個級別。根據兩個因素級別刪除行

str(dt) 
'data.frame': 202206 obs. of 4 variables: 
$ cros : int -205 -200 -195 -190 -185 -180 -175 -170 -165 -160 ... 
$ along: Factor w/ 113 levels "100","101","102",..: 1 1 1 1 1 1 1 1 1 1 ... 
$ alti : num 1.61 1.6 1.6 1.6 1.6 1.59 1.59 1.59 1.59 1.58 ... 
$ year : Factor w/ 6 levels "1979","1983",..: 1 1 1 1 1 1 1 1 1 1 ... 

head(dt) 
cros along alti year 
-205 100 1.61 1979 
-200 100 1.60 1979 
-195 100 1.60 1979 
-190 100 1.60 1979 
-185 100 1.60 1979 
-180 100 1.59 1979 

此數據是來自不同斷面是可變沿,比他們在每5米其是變量測量的斷面CROS高度是可變ALTI信息。他們已經完成了多年,然而有時候該樣本在某一年更長。所以我想刪除那些不是全年測量的交叉點的行。

對於我的數據集,我有一個因子(along),有113個級別,在該因子內,我有因子year和6個級別。在這些數值中,我有x(along)和y(alti),我想在這一年做分析,但是這些年來x必須是相同的值。我希望因子cros可以刪除along的每個因子在所有years上都不會出現的值。

我使用的代碼是:

require(data.table) 
dt <- as.data.table(total) 
tt <- dt[,length(unique(along,year)),by=cros] 
tt <- tt[V1==max(V1)] 
test <-dt[cros %in% tt$cros] 

但我沒有得到正確的結果。我可以想象這種獨特的(一年,一年)並不是使用分組數據的正確方法。但是我不知道該怎麼做。

奧凱這裏是多一點點清楚我想要什麼

> df <- data.frame(along =  c(10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,12,12,12,12), year = c(20,20,20,25,25,25,21,21,20,20,25,25,25,21,21,21,20,20,20,20,25,25,25,25,25,21,21,21,21), cros = c(11,12,13,11,12,13,11,12,11,12,11,12,13,11,12,13,14,15,16,17,14,15,16,17,18,12,13,14,15), value = ceiling(rnorm(29)*10)) 
> df 
    along year cros value 
    10 20 11 -3 
    10 20 12  5 
    10 20 13 -22 
    10 25 11 -9 
    10 25 12 -3 
    10 25 13 -8 
    10 21 11 -8 
    10 21 12 -8 
    11 20 11  7 
    11 20 12 -4 
    11 25 11 -6 
    11 25 12  9 
    11 25 13 -5 
    11 21 11  6 
    11 21 12 17 
    11 21 13 -5 
    12 20 14 -16 
    12 20 15 -17 
    12 20 16 -18 
    12 20 17 -3 
    12 25 14 -18 
    12 25 15 -11 
    12 25 16 -1 
    12 25 17  6 
    12 25 18 14 
    12 21 12 -3 
    12 21 13 19 
    12 21 14 16 
    12 21 15  7 

這是我多麼希望它看起來像,這樣的CRO(X)不爲所有年發生值一個給出的橫斷面被刪除。

along year cros value 
    10 20 11 -3 
    10 20 12  5 
    10 25 11 -9 
    10 25 12 -3 
    10 21 11 -8 
    10 21 12 -8 
    11 20 11  7 
    11 20 12 -4 
    11 25 11 -6 
    11 25 12  9 
    11 21 11  6 
    11 21 12 17 
    12 20 14 -16 
    12 20 15 -17 
    12 25 14 -18 
    12 25 15 -11 
    12 21 14 16 
    12 21 15  7 
+0

您應該給出一個可重複使用的玩具數據示例。例如做一些描述問題的東西,並告訴我們你的期望。與data.table相同的東西(沿= as.factor(rep(1:2,each = 6)),years = rep(2000:2003,times = 4))' –

回答

1

下面是做這件事的一種方式。找到您想要保留的所有along,cros條目,然後將它們合併回來:

dt = data.table(df) 

# find the intersections; run in pieces to see what's going on here 
to.keep = dt[, list(list(unique(cros))), by = list(along, year)][, 
       list(cros = Reduce(intersect, V1)), by = along] 

# set the keys to merge together 
setkey(to.keep, along, cros) 
setkey(dt, along, cros) 

# final result 
res = to.keep[dt, nomatch = 0] 

# optionally, you can order and rearrange columns 
setkey(res, along, year, cros)[, names(dt), with = F] 
# along year cros value 
# 1: 10 20 11 11 
# 2: 10 20 12  7 
# 3: 10 21 11 -4 
# 4: 10 21 12  9 
# 5: 10 25 11 -16 
# 6: 10 25 12  8 
# 7: 11 20 11 17 
# 8: 11 20 12  1 
# 9: 11 21 11  8 
#10: 11 21 12 -13 
#11: 11 25 11 -7 
#12: 11 25 12 17 
#13: 12 20 14 12 
#14: 12 20 15 -7 
#15: 12 21 14  3 
#16: 12 21 15  9 
#17: 12 25 14  6 
#18: 12 25 15 -2 
0
基於我更新了問題的理解

編輯 1沿着是樣ID 2-CRO公司是在給定的斷面在所有年份 3-所有斷面進行了採樣,但不是在所有采樣點採樣點每樣在所有年份 4的問題是,除去那些沒有爲所有六個年

這裏採樣所有采樣點進行採樣是一個方法,以消除那些行:

require(plyr) 
count_var<-ddply(dt, ~along+cros, summarise, count = length(year)) 
str(count_var) 
dt<-merge(dt, count_var, by = c("along", "cros"), all.x = T) 
dt_all6<-subset(dt, count==6) 

從所有年使DT所有取樣點:

along<-as.factor(rep(1:113, 54)) 
year<-as.factor(c(rep(1979, 1017), rep(1980, 1017), rep(1981, 1017), rep(1982, 1017), rep(1983, 1017), rep(1984, 1017))) 
cros_A<-c(rep(5, 113), rep(10, 113), rep(15, 113), rep(20, 113), rep(25, 113), rep(30, 113), rep(35, 113), rep(40, 113), rep(45, 113)) 
cros<-as.factor(rep(cros_A, 6)) 
set.seed(2) 
alti<-rnorm(6102, mean = 1.5, sd = 0.5) 
dt<-cbind.data.frame(along, year, cros, alti) 

現在去掉一些採樣點:

dt<-dt[c(1:100, 106:400, 406:1500, 1506:1600, 1606:2500, 2506:3000, 3006:3500, 3506:4000, 4006:5000, 5006:6102), ] 
+0

這是行不通的,最後一行的代碼只是給了我一個空的數據框。我想刪除每年不會發生的變量cros的特定行,並且每個transect除外。 – Marinka

+0

但是我的樣本數據集在他們中有一些錯誤(我忘記刪除行號行,不知何故每年和我轉身) – Marinka

+0

交叉實際的觀測變量?還是僅僅是你已經做出的一項任務來表明是否存在給定水平的所有年份?我假設後者。如果這不正確,則重命名長度變量。 – SamanthaDS