简体   繁体   English

零作为R中的遗漏案例

[英]Zeros as missing cases in R

I have a csv with millions of cases that look like this: 我有一个csv,包含数百万个如下所示的案例:

Case_1,11,17481,172,4436,8,4436
Case_2,11,1221,680,55200,1776,55200
Case_3,16,6647,6449,579967,1,579967
Case_4,22,0,0,0,0,0

In this case, Case_4 is missing data, since it has a bunch of zeros in it (there are hundreds of these in the file). 在这种情况下, Case_4缺少数据,因为其中包含一堆零(文件中有数百个)。 I'm very new to R, and I was wondering if there is an efficient way of deleting these kinds of missing data from the file? 我是R的新手,我想知道是否有一种有效的方法可以从文件中删除这些丢失的数据? Thanks. 谢谢。

读取文件时,请使用na.strings参数。

df <- read.csv("filename.csv", na.strings="0")

if you want to replace all your zeros with missing values than. 如果要用缺失值than替换所有零。

x = data.frame(dataset) x[x==0] = NA

Where dataset is the variable where you have saved the csv file 其中数据集是您保存了csv文件的变量

To delete the rows which have 0 entries (as desired by OP): 要删除具有0个条目的行(OP希望):

ddf[ddf==0]=NA
ddf = ddf[complete.cases(ddf),]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM