2015-03-03 58 views
1

我有以下的數據幀:爲什麼不在數據框中刪除行爲零?

dat <- structure(list(V1 = structure(c(11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("XXX_LN_06.ID", 
"xxx_LN_06.ID", "aaa_LN_06.ID", "bbb_LN_06.ID", "ccc_LN_06.ID", 
"ddd_LN_06.ID", "eee_LN_06.ID", "fff_LN_06.ID", "ggg_LN_06.IN", 
"hhh_LN_06.ID", "iii_LN_06.ID", "jjj_LN_06.ID", "kkk_LN_06.ID", 
"lll_LN_06.ID", "mmm_LN_06.ID", "nnn_LN_06.ID", "ooo_LN_06.ID", 
"ppp_LN_06.ID", "qqq_IC_LN_06.ID", "rrr_LN_06.ID", "sss_LN_06.ID" 
), class = "factor"), V2 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("Bcells", 
"DendriticCells", "Macrophages", "Monocytes", "NKCells", "Neutrophils", 
"StemCells", "StromalCells", "abTcells", "gdTCells"), class = "factor"), 
    V3 = c(4474.2737, 5893.97307, 9414.21112, 5743.65136, 4100.84016, 
    7280.7078, 5317.92682, 11905.14762, 4697.03516, 4661.754, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), V4 = c(1.501, 1.978, 3.159, 
    1.927, 1.376, 2.443, 1.785, 3.995, 1.576, 1.564, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0)), .Names = c("V1", "V2", "V3", "V4"), row.names = 191:210, class = "data.frame") 

顯示爲:

> dat 
       V1    V2  V3 V4 
191 iii_LN_06.ID   Bcells 4474.274 1.501 
192 iii_LN_06.ID DendriticCells 5893.973 1.978 
193 iii_LN_06.ID Macrophages 9414.211 3.159 
194 iii_LN_06.ID  Monocytes 5743.651 1.927 
195 iii_LN_06.ID  NKCells 4100.840 1.376 
196 iii_LN_06.ID Neutrophils 7280.708 2.443 
197 iii_LN_06.ID  StemCells 5317.927 1.785 
198 iii_LN_06.ID StromalCells 11905.148 3.995 
199 iii_LN_06.ID  abTcells 4697.035 1.576 
200 iii_LN_06.ID  gdTCells 4661.754 1.564 
201 ggg_LN_06.IN   Bcells  0.000 0.000 
202 ggg_LN_06.IN DendriticCells  0.000 0.000 
203 ggg_LN_06.IN Macrophages  0.000 0.000 
204 ggg_LN_06.IN  Monocytes  0.000 0.000 
205 ggg_LN_06.IN  NKCells  0.000 0.000 
206 ggg_LN_06.IN Neutrophils  0.000 0.000 
207 ggg_LN_06.IN  StemCells  0.000 0.000 
208 ggg_LN_06.IN StromalCells  0.000 0.000 
209 ggg_LN_06.IN  abTcells  0.000 0.000 
210 ggg_LN_06.IN  gdTCells  0.000 0.000 

我想要做的就是用零去除。 產量

> dat 
       V1    V2  V3 V4 
191 iii_LN_06.ID   Bcells 4474.274 1.501 
192 iii_LN_06.ID DendriticCells 5893.973 1.978 
193 iii_LN_06.ID Macrophages 9414.211 3.159 
194 iii_LN_06.ID  Monocytes 5743.651 1.927 
195 iii_LN_06.ID  NKCells 4100.840 1.376 
196 iii_LN_06.ID Neutrophils 7280.708 2.443 
197 iii_LN_06.ID  StemCells 5317.927 1.785 
198 iii_LN_06.ID StromalCells 11905.148 3.995 
199 iii_LN_06.ID  abTcells 4697.035 1.576 
200 iii_LN_06.ID  gdTCells 4661.754 1.564 

爲什麼這不工作?

row_sub = apply(dat, 1, function(row) any(row ==0)) 
dat[row_sub,] 
+5

可能是因爲'apply'首先將dat轉換成字符矩陣 – Cath 2015-03-03 11:12:16

+3

你必須做'dat [!apply(dat [, - (1:2)],1,function(row)any(row == 0) ),]' – akrun 2015-03-03 11:16:07

回答

4

因爲apply首先將數據轉換爲字符。你可以(也應該)首先調試這些東西,就像這樣:

apply(dat, 1, function(row) { print(str(row)) }) 

和輸出的部分是這樣的:

NULL 
Named chr [1:4] "ggg_LN_06.IN" "StromalCells" " 0.000" "0.000" 
- attr(*, "names")= chr [1:4] "V1" "V2" "V3" "V4" 

在那裏你可以很容易地看到它的所有字符。

4

你可以試試這個:

a <- which(dat==0, arr.ind=T) 

dat[-a[,1],] 

或按@大衛下面的評論:

dat[rowSums(dat == 0L) == 0L, ] 

或者:

dat[!rowSums(dat == 0L), ] 

輸出:

> dat[-a[,1],] 
       V1    V2  V3 V4 
191 iii_LN_06.ID   Bcells 4474.274 1.501 
192 iii_LN_06.ID DendriticCells 5893.973 1.978 
193 iii_LN_06.ID Macrophages 9414.211 3.159 
194 iii_LN_06.ID  Monocytes 5743.651 1.927 
195 iii_LN_06.ID  NKCells 4100.840 1.376 
196 iii_LN_06.ID Neutrophils 7280.708 2.443 
197 iii_LN_06.ID  StemCells 5317.927 1.785 
198 iii_LN_06.ID StromalCells 11905.148 3.995 
199 iii_LN_06.ID  abTcells 4697.035 1.576 
200 iii_LN_06.ID  gdTCells 4661.754 1.564 

問題,你的情況:

在你的情況row_sub只是FALSEs一個矢量,所以它不會返回任何行。行向量返回TRUE。

> row_sub 
    191 192 193 194 195 196 197 198 199 200 201 202 203 
FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE 
    204 205 206 207 208 209 210 
FALSE FALSE FALSE FALSE FALSE FALSE FALSE 
+2

或類似的'dat [rowSums(dat == 0L)== 0L,]'或'dat [!rowSums(dat == 0L),]' – 2015-03-03 12:02:43

+1

@DavidArenburg謝謝David!好的。 – LyzandeR 2015-03-03 12:06:43

相關問題