2017-08-24 66 views
0

我在R中有一個字符串向量,它的一些索引是chr(0)。我想打印那些chr(0)的索引,然後刪除它們。我無法完成這兩項任務中的任何一項。我嘗試了第一個方案是3個不同的解決方案:打印並刪除字符串列表中的chr(0)條目

(1) temp <- keepColumns[keepColumns == character(0)] 
(2) temp2 <- which(keepColumns[]== "0") 
(3) temp2 <- foreach(i=1:length(keepColumns)) %do% if (length(keepColumns[i]) == 0) print("Empty") 
#if (identical(keepColumns[i],character(0))) { print(i) } 

在所有情況下,溫度的價值原來是

> temp2 
integer(0) 

要刪除CHR(0),我已經試過類似的解決方案:

keepColumnsList = keepColumns[!identical(keepColumns, character(0))] 

但keepColumnsList與原始向量相同。

列表如下:

> keepColumns 
[[1]] 

> keepColumns 
[[1]] 
[1] "P2.11FIC2026_PV" 

[[2]] 
[1] "P2.11FIC2046D_PV" 

[[3]] 
[1] "P2.11FI2046" 

[[4]] 
[1] "P2.11FY2048I" 

[[5]] 
[1] "P2.11FIC2030_PV" 

[[6]] 
[1] "P2.11FIC2011A_PV" 

[[7]] 
[1] "P2.11FIC2017_OP" 

[[8]] 
[1] "P2.11HIC5001" 

[[9]] 
[1] "P2.11HIC5002" 

[[10]] 
[1] "P2.11HIC5003" 

[[11]] 
[1] "P2.11PI5014" 

[[12]] 
[1] "P2.11TIC5003_PV" 

[[13]] 
[1] "P2.11TIC5011_PV" 

[[14]] 
[1] "P2.11FIC5011_PV" 

[[15]] 
character(0) 

[[16]] 
[1] "P2.11TI5001" 

[[17]] 
[1] "P2.11PIC2031_PV" 

[[18]] 
[1] "P2.11PIC2045_PV" 

[[19]] 
[1] "P2.11HIC2026E" 

[[20]] 
[1] "P2.11AI2001A1" 

[[21]] 
[1] "P2.11AI2001A7" 

[[22]] 
[1] "P2.11FI2029" 

[[23]] 
[1] "P2.11AI2026" 

[[24]] 
[1] "P2.11AI2001A8" 

[[25]] 
[1] "P2.11TI2076" 

[[26]] 
[1] "P2.11TI2068" 

[[27]] 
[1] "P2.11TI2027" 

[[28]] 
[1] "P2.11TI2071" 

[[29]] 
[1] "P2.11AI2001G6" 

[[30]] 
[1] "P2.11TI2047" 

[[31]] 
[1] "P2.11AI2001G1" 

[[32]] 
[1] "P2.11AI2001G2" 

[[33]] 
[1] "P2.11AI2001G3" 

[[34]] 
[1] "P2.11AI2001G4" 

[[35]] 
[1] "P2.11AI2001G5" 

[[36]] 
[1] "P2.11AI2001H5" 

[[37]] 
[1] "P2.11FI5001" 

[[38]] 
[1] "P2.11FI5021" 

[[39]] 
[1] "P2.11FI5023" 

[[40]] 
[1] "P2.11PI5004" 

[[41]] 
[1] "P2.11TI5009" 

[[42]] 
[1] "P2.11TI5010" 

[[43]] 
[1] "P2.11TI5026" 

[[44]] 
[1] "P2.11TI5034" 

[[45]] 
[1] "P2.11TI5036" 

[[46]] 
[1] "P2.11TI5038" 

[[47]] 
[1] "P2.11TI5045" 

[[48]] 
[1] "P2.11FI2001A" 

[[49]] 
[1] "P2.11FI2001B" 

[[50]] 
character(0) 

[[51]] 
character(0) 

[[52]] 
[1] "P2.11TI2061" 

[[53]] 
[1] "P2.11TI2062" 

[[54]] 
[1] "P2.11TI2063" 

[[55]] 
[1] "P2.11TI2064" 

[[56]] 
[1] "P2.11TI2065" 

[[57]] 
[1] "P2.11TI2066" 
+0

它看起來像'keepColumns'是一個列表,是真的嗎? '類(keepColumns)'? – Nate

+1

你有一個列表。嘗試:'Filter(length,keepColumns)'獲取不等於'character(0)'的元素,如果你想打印那些等於'character(0)'的元素,那麼你可以做'Filter(function(x )length(x)== 0,keepColumns)'。 – Abdou

+0

是的,它是一個「列表」 –

回答

0

這適用於去除與CHR的條目(0)值:

Filter(function(x) length(x)==0, keepColumns) 

要顯示該列表的索引,其中chr(0)值是,以下應該工作:

Filter(function(x) length(keepColumns[[x]])==0, seq_along(keepColumns)) 

但仍然沒有解決方案顯示所有chr(0)條目的索引號在向量 中。