Find Character Pattern in Data Frame Column (2 Examples) | Test, Check & Identify | str_detect()

preview_player
Показать описание
R code of this video:

x2 = c("foo", "foo", "bar", "foo", "bar"))
data # Print example data frame

grepl("bar", data$x2) # Return logical vector using grepl

data_new1 <- data[grepl("bar", data$x2), ] # Subset data frame
data_new1 # Print data frame subset

library("stringr") # Load stringr

str_detect("bar", data$x2) # Return logical vector using str_detect

data_new2 <- data[str_detect("bar", data$x2), ] # Subset data frame
data_new2 # Print data frame subset

Follow me on Social Media:

Рекомендации по теме
Комментарии
Автор

Thanks for this tutorial. Suppose you have a table that contains the minutes goals were scored during a match and you want to count how many goals were scored in the first fifteen minutes (1-15) of the game. How do you write the code for that? Data set sample below

Goals_Minutes Result
10, 15, 20 1 - 2
18, 12, 35 3 - 0

The result of this sample should be 3 goals because of minutes 10, 15 and 12.

Thanks

jamesleleji
visit shbcf.ru