Delete Rows Containing Specific Strings in R

Delete rows containing specific strings in R

This should do the trick:

df[- grep("REVERSE", df$Name),]

Or a safer version would be:

df[!grepl("REVERSE", df$Name),]

Remove row if any column contains a specific string

We can use rowSums to create a logical vector and subset based on it

df1[rowSums(df1 == "no_data")==0, , drop = FALSE]
# time speed wheels
#4 3:00 50 18

data

df1 <- structure(list(time = c("1:00", "2:00", "no_data", "3:00"), speed = c("30", 
"no_data", "no_data", "50"), wheels = c("no_data", "18", "no_data",
"18")), .Names = c("time", "speed", "wheels"), class = "data.frame",
row.names = c(NA, -4L))

Remove Rows occurring after a String R Data frame

We can subset with row_numberand which

library(dplyr)

df %>% filter(row_number() < which(A=='total'))

A B
1 Bob Smith 01005
2 Carl Jones 01008
3 Syndey Lewis 01185

Remove rows which have less than string into a specific column

If you want to use tidyverse packages you could use:

library(dplyr)
library(stringr)

dd %>% filter(str_count(text, " ") >= 3)

Here we assume that "less than 4 strings" means less than 3 spaces. By counting characters, you can have a much more efficient solution than actually going though the work of splitting the string up and allocating the memory for the separate pieces when you don't really need them.

Remove Rows From Data Frame where a Row matches a String

Just use the == with the negation symbol (!). If dtfm is the name of your data.frame:

dtfm[!dtfm$C == "Foo", ]

Or, to move the negation in the comparison:

dtfm[dtfm$C != "Foo", ]

Or, even shorter using subset():

subset(dtfm, C!="Foo")

Deleting rows corresponding to particular string, but keep NA values

We can create a condition with is.na to return those elements that are NA as well with |

 subset(df, Col1 != 'string' | is.na(Col1))

Delete rows based on two columns (& containing specific strings)

Does this work:

library(dplyr)
library(stringr)

report %>% group_by(Trial) %>% filter(all(str_detect(Code, 'test', negate = TRUE)))
# A tibble: 10 x 5
# Groups: Trial [2]
Subject Trial `Event type` Code Time
<chr> <dbl> <chr> <chr> <chr>
1 VP02_RP 22 Picture face01_n 1700595
2 VP02_RP 22 Sound mnegsound1 1700886
3 VP02_RP 22 Picture pospic1 1716953
4 VP02_RP 22 Nothing ev_mnegposneg_adj_onset 1748603
5 VP02_RP 22 Response 14 1779212
6 VP02_RP 25 Picture face02_n 1801100
7 VP02_RP 25 Sound fpossound1 1801392
8 VP02_RP 25 Picture negpic1 1816560
9 VP02_RP 25 Nothing ev_fnegnegpos_adj_onset 1846190
10 VP02_RP 25 Response 15 1877413


Related Topics



Leave a reply



Submit