本文介绍了R删除包含一定值的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以它有一个csv我正在读入一个R数据帧,它看起来像这样

  clientx,clienty,screenx ,screeny 
481,855,481,847
481,784,481,847
481,784,481,847
879,292,879,355

第一行当然是标题。所以我们有4列数字数据,范围从1到4位数。除了-1表示缺失值,集合中没有负数。
我想删除在4列中的任何一行包含-1的每一行。



提前感谢帮助

解决方案

你最有效的方式是使用 > read.csv()将所有 -1 值编码为 NA ,然后删除不完整的情况。






步骤1:设置 na.strings = $ code code code code code code code code $ c> x< - read.csv(text =
clientx,clienty,screenx,screeny
481,855,481,847
481,784,481,847
481,784,481,847
-1,292,879,355,标题= TRUE,na.strings = -1)

x
clientx clienty screenx screeny
1 481 855 481 847
2 481 784 481 847
3 481 784 481 847
4 NA 292 879 355






步骤2:现在使用 complete.cases na.omit

  x [complete.cases(x ),] 
clientx clienty screenx screeny
1 481 855 481 847
2 481 784 481 847
3 481 784 481 847

na.omit( x)
clientx clienty screenx screeny
1 481 855 481 847
2 481 784 481 847
3 481 784 481 847
pre>



So it got a csv I'm reading into an R dataframe, it looks like this

clientx,clienty,screenx,screeny
481,855,481,847
481,784,481,847
481,784,481,847
879,292,879,355

First line is of course the header. So we have 4 columns with numeric data in it, ranging from 1 to 4 digits. There are no negative numbers in the set except -1 which marks a missing value.I want to remove every row that contains a -1 in any of the 4 columns.

Thanks in advance for the help

解决方案

Your most efficient way will be to use the na.strings argument of read.csv() to code all -1 values as NA, then to drop incomplete cases.


Step 1: set na.strings=-1 in read.csv():

x <- read.csv(text="
clientx,clienty,screenx,screeny
481,855,481,847
481,784,481,847
481,784,481,847
-1,292,879,355", header=TRUE, na.strings=-1)

x
clientx clienty screenx screeny
1     481     855     481     847
2     481     784     481     847
3     481     784     481     847
4      NA     292     879     355


Step 2: Now use complete.cases or na.omit:

x[complete.cases(x), ]
  clientx clienty screenx screeny
1     481     855     481     847
2     481     784     481     847
3     481     784     481     847

na.omit(x)
  clientx clienty screenx screeny
1     481     855     481     847
2     481     784     481     847
3     481     784     481     847


这篇关于R删除包含一定值的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 09:32