如何从包含特定列中的符号的data.frame中删除行 [英] How to remove rows from a data.frame containing a symbol in a particular column

查看:168
本文介绍了如何从包含特定列中的符号的data.frame中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从名为 mydata 的数据框中删除所有行,其中名为的列第一个包含一个 +。这是R中的正确命令?



这是我的数据框如下所示:

 会话第一最后城市
1 9cf571c8faa67cad2aa9ff41f3a26e38猫biddix弗雷斯诺
2 e30f853d4e54604fd62858badb68113a迦勒+乔伊·阿莫斯
3分配2ad41134cc285bcc06892fd68a471cd7丹尼尔·福克斯
4分配2ad41134cc285bcc06892fd68a471cd7丹尼尔·福克斯
5 63a5e839510a647c1ff3b8aed684c2a5查尔斯·皮尔斯火石
6分配691df47f2df12f14f000f9a17d1cc40e J +亨利·弗兰茨·普雷斯科特+谷
7分配691df47f2df12f14f000f9a17d1cc40e J +亨利·弗兰茨·普雷斯科特+谷
8分配b3a1476aa37ae4b799495256324a8d3d卡丽·马斯科罗面包机
9分配bd9f1404b313415e7e7b8769376d2705弗雷德·摩拉里斯拉斯+维加斯
10 b50a610292803dc302f24ae507ea853a aurora lee
11 fb74940e6feb0dc61a1b4d09fcbbcb37安德鲁价格约克ville


解决方案

grep!

  mydata<  -  read.table(textConnection(session first last city 
9cf571c8faa67cad2aa9ff41f3a26e38 cat biddix fresno
e30f853d4e54604fd62858badb68113a caleb + joey amos blah
63a5e839510a647c1ff3b8aed684c2a5 me + you amos blah),header = T,stringsAsFactors = FALSE)

grep(\\ +,mydata $ first)

其中返回

  [1] 2 3 

告诉你第2列,第2行和第3行在其中有一个+ / p>

所以你可以运行:

  mydata<  -  mydata [ - grep(\\ +,mydata $ first),] 
mydata

那些整行将被删除。不确定是否是您的问题的错字,但您说您想要删除第一列中的行,您的意思是条目或整行?


I would like to remove all the rows from a dataframe named mydata where the column named first contains a "+". What is the correct command for this in R?

Here is what my dataframe looks like:

session                              first     last           city
1  9cf571c8faa67cad2aa9ff41f3a26e38     cat   biddix          fresno
2  e30f853d4e54604fd62858badb68113a caleb+joey amos                
3  2ad41134cc285bcc06892fd68a471cd7  daniel  folkers                
4  2ad41134cc285bcc06892fd68a471cd7  daniel  folkers                
5  63a5e839510a647c1ff3b8aed684c2a5 charles   pierce           flint
6  691df47f2df12f14f000f9a17d1cc40e j+henry    franz prescott+valley
7  691df47f2df12f14f000f9a17d1cc40e j+henry    franz prescott+valley
8  b3a1476aa37ae4b799495256324a8d3d  carrie mascorro            brea
9  bd9f1404b313415e7e7b8769376d2705    fred  morales       las+vegas
10 b50a610292803dc302f24ae507ea853a  aurora      lee                
11 fb74940e6feb0dc61a1b4d09fcbbcb37  andrew    price       yorkville

解决方案

grep!

mydata <- read.table(textConnection("session  first  last  city
9cf571c8faa67cad2aa9ff41f3a26e38  cat biddix  fresno
e30f853d4e54604fd62858badb68113a  caleb+joey  amos  blah
63a5e839510a647c1ff3b8aed684c2a5  me+you  amos  blah"), header=T, stringsAsFactors=FALSE)

grep("\\+",mydata$first)

which returns

[1] 2 3

Telling you in column 2, rows 2 and 3 have a '+' in them.

So you could run:

mydata <- mydata[-grep("\\+",mydata$first),]
mydata

And those entire rows would be deleted. Not sure if it's a typo in your question or not but you say you want to "remove the rows in the first column", do you mean the entries or the entire row?

这篇关于如何从包含特定列中的符号的data.frame中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆