Zeros as missing cases in R

╄→гoц情女王★ 提交于 2019-12-11 07:42:29

问题


I have a csv with millions of cases that look like this:

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). 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? Thanks.


回答1:


Use the na.strings argument when reading in your file.

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



回答2:


if you want to replace all your zeros with missing values than.

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

Where dataset is the variable where you have saved the csv file




回答3:


To delete the rows which have 0 entries (as desired by OP):

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


来源:https://stackoverflow.com/questions/26957742/zeros-as-missing-cases-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!