为什么当我删除特定行时,我的输出都是NA?

时间:2016-02-12 11:56:40

标签: r

我有一个我在这里上传的数据 https://gist.github.com/anonymous/0bc36ec5f46757de7c2c

我使用以下命令将其加载到R中

df <- read.delim("path to the data", header=TRUE, sep="\t", fill=TRUE, row.names=1, stringsAsFactors=FALSE, na.strings='') 

然后我检查一个特定的列,看看有多少+就像这样

length(which(df$Potential.contaminant == "+")) 

在这个cas中显示9。然后我尝试使用以下命令

删除该行中+的所有行
Newdf <- df[df$Potential.contaminant != "+", ] 

输出全部为NA。怎么了 ??我在这做错了什么?

正如@akrun建议我尝试了许多不同的方法,但没有成功

df[!grepl("[+]", df$Potential.contaminant),]
df[ is.na(df$Potential.contaminant),]
subset(df, Potential.contaminant != "+")
df[-(which(df$Potential.contaminant == "+")),] 

上述命令均无法解决问题。一个想法是,潜在污染物具有NA,这就是原因。我使用

将所有NA替换为零
df[c("Potential.contaminant")][is.na(df[c("Potential.contaminant")])] <- 0

但仍然相同。

2 个答案:

答案 0 :(得分:0)

将您的要点复制粘贴到文件c:/input.txt中,然后使用您的代码:

df <- read.delim("c:/input.txt", header=TRUE, sep="\t", fill=TRUE, row.names=1, stringsAsFactors=FALSE, na.strings='') 

现在:

> str(df)
'data.frame':   21 obs. of  11 variables:
 $ Intensityhenya         : int  0 NA NA NA NA 0 0 0 0 0 ...
 $ Only.identified.by.site: chr  "+" NA NA NA ...
 $ Reverse                : logi  NA NA NA NA NA NA ...
 $ Potential.contaminant  : chr  "+" NA NA NA ...
 $ id                     : int  0 NA NA NA NA 1 2 3 4 5 ...
 $ IDs.1                  : chr  "16182;22925;28117;28534;28538;29309;36387;36889;42536;49151;49833;52792;54591;54592" NA NA NA ...
 $ razor                  : chr  "True;True;False;False;False;False;False;True;False;False;False;False;False;False" NA NA NA ...
 $ Mod.IDs                : chr  "16828;23798;29178;29603;29607;30404;38270;38271;38793;44633;51496;52211;55280;57146;57147;57148;57149" NA NA NA ...
 $ Evidence.IDs           : chr  "694702;694703;694704;1017531;1017532;1017533;1017534;1017535;1017536;1017537;1017538;1017539;1017540;1017541;1017542;1017543;10"| __truncated__ NA NA NA ...
 $ GHSIDs                 : chr  NA NA NA NA ...
 $ BestGSFD               : chr  NA NA NA NA ...

如果我尝试分组:

> df2 <- df[is.na(df$Potential.contaminant),]
> str(df2)
'data.frame':   12 obs. of  11 variables:
 $ Intensityhenya         : int  NA NA NA NA NA NA NA NA NA NA ...
 $ Only.identified.by.site: chr  NA NA NA NA ...
 $ Reverse                : logi  NA NA NA NA NA NA ...
 $ Potential.contaminant  : chr  NA NA NA NA ...
 $ id                     : int  NA NA NA NA NA NA NA NA NA NA ...
 $ IDs.1                  : chr  NA NA NA NA ...
 $ razor                  : chr  NA NA NA NA ...
 $ Mod.IDs                : chr  NA NA NA NA ...
 $ Evidence.IDs           : chr  NA NA NA NA ...
 $ GHSIDs                 : chr  NA NA NA NA ...
 $ BestGSFD               : chr  NA NA NA NA ...

但你的数据太疯狂了,几乎不可能想象它们,所以让我们尝试别的东西来了解它。

> colnames(df)
 [1] "Intensityhenya"          "Only.identified.by.site" "Reverse"                 "Potential.contaminant"   "id"                      "IDs.1"                   "razor"                   "Mod.IDs"                
 [9] "Evidence.IDs"            "GHSIDs"                  "BestGSFD"  

您的标题很难理解,让我们来看看它:

IDs Intensityhenya  Only identified by site Reverse Potential contaminant   id  IDs razor   Mod.IDs Evidence IDs    GHSIDs  BestGSFD

除了一系列数据,其中长数据被削减以便一目了然:

CON__A2A4G1 0   +       +   0   16182;[...];4592    True;[..];False 16828;[...];57149   694702;[...];2208697;       
208698;[...];2441826                                            
3;2433194;[...];4682766                                     

我刚刚删除了多余的数字,确保保留标签和换行符。

我希望您了解如何以及为什么这样可以对数据进行正确分析,在重新加载R之前,请检查输入数据以对其进行清理。

为了便于说明,这里有你的要点,省略号和%T%代替标签:

IDs%T%Intensityhenya%T%Only identified by site%T%Reverse%T%Potential contaminant%T%id%T%IDs%T%razor%T%Mod.IDs%T%Evidence IDs%T%GHSIDs%T%BestGSFD
CON__A2A4G1%T%0%T%+%T%%T%+%T%0%T%1618[...]4592%T%Tru[...]alse%T%1682[...]7149%T%69470[...]208697;%T%%T%
20869[...]441826%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%
[...]20%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%
00[...]%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%
1271[...]682766%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%
CON__A2A5Y0%T%0%T%%T%%T%+%T%1%T%443[...]5777%T%Fals[...]rue%T%464[...]8377%T%21071[...]489947%T%40503[...]780178%T%40505[...]780175
CON__A2AB72%T%0%T%%T%%T%+%T%2%T%443[...]0447%T%Tru[...]alse%T%464[...]2842%T%21070[...]232341%T%40502[...]250729%T%40502[...]250728
CON__ENSEMBL:ENSBTAP00000014147%T%0%T%%T%%T%+%T%3%T%53270%T%TRUE%T%55779%T%238286[...]382871%T%457377[...]573778%T%4573776
CON__ENSEMBL:ENSBTAP00000024146%T%0%T%%T%%T%+%T%4%T%186[...]5835%T%Tru[...]rue%T%194[...]8438%T%8382[...]492132%T%15455[...]783465%T%15455[...]783465
CON__ENSEMBL:ENSBTAP00000024466;CON__ENSEMBL:ENSBTAP00000024462%T%0%T%%T%%T%+%T%5%T%939[...]5179%T%Tru[...]rue%T%978[...]7757%T%41149[...]468480%T%78212[...]739209%T%78217[...]739209
CON__ENSEMBL:ENSBTAP00000025008%T%0%T%+%T%%T%+%T%6%T%1564[...]8580%T%Fals[...]alse%T%1627[...]9651%T%66672[...]269215%T%125151[...]439696%T%125151[...]439691
CON__ENSEMBL:ENSBTAP00000038253%T%0%T%%T%%T%+%T%7%T%120[...]5703%T%Fals[...]alse%T%125[...]8300%T%5326[...]25602%T%%T%
;125602[...]178%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%
1[...]483384%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%
22838[...]23247%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%
;123247[...]411%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%
4[...]7%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%
603[...]790126;%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%
79012[...]13848%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%
;413848[...]765024%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%%T%
sp|O43790|KRT86_HUMAN;CON__O43790%T%0%T%%T%%T%+%T%8%T%121[...]5716%T%Tru[...]rue%T%126[...]8315%T%5455[...]484318%T%10404[...]426334%T%

答案 1 :(得分:0)

您的数据行似乎没有标记为污染物,但没有值。 “NA”是因为read.delim函数调用期间的“na.strings =''”。例如,如果你这样做:

df <- read.delim("https://gist.githubusercontent.com/anonymous/0bc36ec5f46757de7c2c/raw/517ef70ab6a68e600f57308e045c2b4669a7abfc/example.txt", header=TRUE, row.names=1, sep="\t")
df<-df[df$Potential.contaminant!='+',] 
summary(df)

你应该看到空单元格。