2016-02-24 68 views
-2

值子集的數據幀我有這樣的數據幀:包含從陣列

'data.frame': 114034 obs. of 4 variables: 
$ Ore  : chr "01 00" "01 01" "01 02" "01 03" ... 
$ SquareID: chr "10000" "10000" "10000" "10000" ... 
$ Intens : num 0.0118 0.00987 0.00538 0.01318 0.00273 ... 
$ Count : num 69 78 51 86 35 ... 

我需要子集的全部數據幀。 SquareID等於那些數組值的數據幀的行。

Df<-subset.data.frame(Df,Df$SquareID==c(4702,4703,4704,4705,5434,5435,4706,4707,4708,4709,4820,4939,4821,4822,5551,5057,4823,5058,4824,4825,5059,4826,5174,4940,4941,5175,4942,4943,5177,5178,4944,5060,4945,5061,4946,5062,5063,4728,5295,5296,5297,5180,5181,4845,4846,4847,4963,4964,5199,4353,5082,4355,4356,4585,9536,4586,4587,4470,4588,4471,4589,4472,4473,5412,5413,5414,9653,4590,4591,5530,5315,5316,5318) 

這樣,我得到了這樣的警告和錯誤的結果:

更長對象長度不短對象長度的倍數

+2

請參閱%「'中的幫助文件'?」%。 – nrussell

+0

我需要子集整個數據幀,我不需要一個向量作爲結果 – DanieleO

+1

正確的,子集(Df,%c(...))中的SquareID%)。你的'SquareID'列是'character',但你應該先將它轉換爲'numeric'。 – nrussell

回答

1

nrussell是正確的。
假設你有一個數據幀

> df <- data.frame(SquareId = c("1","2","3","4"), Intens = c(15,30,45,60)) 
> df 
    SquareId Intens 
1  1  15 
2  2  30 
3  3  45 
4  4  60 

你可以子集是這樣的:

> df <- subset(df, SquareId %in% c("2","3")) 
> df 
    SquareId Intens 
2  2  30 
3  3  45 
1

的問題是在該行的邏輯比較:

Df<-subset.data.frame(Df,Df$SquareID==c(4702,4....)) 

Df$SquareID是一個向量長度114034(數據幀的行數),而c(4702,4....)是長度爲73的向量,因此longer object length is not a multiple of shorter object length。按照nrussell的建議,你需要%。

Df<-subset(Df,Df$SquareID %in% c(4702,4....))