我在R中有一个字符串向量,它的一些索引是chr(0)。我想打印那些chr(0)的索引,然后删除它们。我无法完成这两项任务中的任何一项。我在第一个场景中尝试的是3种不同的解决方案:
(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) }
在所有情况下,temp的值都是
> 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 :(得分:0)
这适用于删除带有chr(0)值的条目:
Filter(function(x) length(x)==0, keepColumns)
要显示chr(0)
值所在列表的索引,以下内容应该有效:
Filter(function(x) length(keepColumns[[x]])==0, seq_along(keepColumns))
但仍无法显示向量中所有chr(0)条目的索引号。