2017-06-14 76 views
-7

我試圖從數據集中刪除幾個元素。它有A,B,C,1,2,3,4,5爲它的內容:從R中的數據集中刪除內容

>dataset 
    [1] A 4 3 C 3 3 3 C 3 B 3 4 3 3 3 B 3 3 5 3 3 4 A 3 3 5 3 3 4 3 2 3 C 6 A 3 3 
    [38] 3 A 3 3 A 3 3 3 3 3 A 3 C B 3 B 3 A 3 1 8 1 1 C 1 1 3 3 3 3 B 3 A A 3 5 3 

我想從數據集中刪除所有 「A」 秒和 「B」 S。 預期的數據集應該只有1,2,3,4,5,C作爲其元素。

我曾嘗試用以下代碼,但未能取得成功:

>rm(dataset$"B") # to remove "B"s 


> x.sub <- subset(dataset, "B" > 1) #to remove Bs appearing more than once 

你知道我怎麼能刪除?

+1

你應該包括[再現的示例](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)與在複製樣本的輸入數據/可粘貼格式。還要爲樣本輸入提供所需的輸出。也許從[R的介紹](https://cran.r-project.org/doc/manuals/r-release/R-intro.html)開始,因爲你似乎缺少一些基礎知識。 – MrFlick

回答

1
dataset <- dataset[!(dataset %in% c('A','B'))]