问题
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
回答1:
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?
来源:https://stackoverflow.com/questions/8424540/how-to-remove-rows-from-a-data-frame-containing-a-symbol-in-a-particular-column