TDaw
TDaw

Reputation: 203

Filter by character, creating column with results

I am trying to create a graph showing two data sets, if it is that character or if it is not that character.

Currently I am working with something like:

setosa <-  iris%>%
  filter_all(any_vars(str_detect(., pattern = "setosa")))

Using the iris example I would like to be able to search out a character in a string eg: setosa, then draw a graph showing setosa and not setosa.

See below an example of my data frame, the pd.Sails column is full off different connotations of the same character. I want to be able to draw a graph of pd$TWA v pd.TWS and the data shows results from the pd$Sails column based on name, so J2 N or not J2 N

Sorry for moving away from Iris I was struggling to convey the problem!

My current df is:


 pd.Boat   pd.Sails pd.TWA pd.TWS
1  Sojana RMAIN/J2 N   40.9   13.7
2  Sojana RMAIN/J2 N   38.8   13.0
3  Sojana RMAIN/J2 N   37.8   13.3
4  Sojana RMAIN/J2 N   37.3   13.3
5  Sojana RMAIN/J2 N   45.2   13.2
6  Sojana RMAIN/J2 N   50.6   13.2

dput(head)

structure(list(pd.Boat = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Sojana", class = "factor"), 
    pd.Sails = structure(c(36L, 36L, 36L, 36L, 36L, 36L), .Label = c("RMAIN", 
    "RMAIN/A2", "RMAIN/A2 C - north 19", "RMAIN/A2 C - north 19/Big Jonny", 
    "RMAIN/A2 C - north 19/CMiz SS", "RMAIN/A2 C - north 19/Spi SS v2/Big Jonny", 
    "RMAIN/A2 Doyle", "RMAIN/A2 Doyle/Big Jonny", "RMAIN/A2 Doyle/Spi SS v2", 
    "RMAIN/A2 Doyle/Spi SS v2/Big Jonny", "RMAIN/A2 N", "RMAIN/A2 N/Big Jonny", 
    "RMAIN/A2 N/Mizzen SS V2", "RMAIN/A2 N/Spi SS v2/Big Jonny", 
    "RMAIN/A2/Big Jonny", "RMAIN/A2/Big Jonny/Spi SS v2", "RMAIN/A2/Mizzen SS V2", 
    "RMAIN/A2/Spi SS v2", "RMAIN/A2/Spi SS v2/Big Jonny", "RMAIN/A2/Spi SS v2/Mizzen SS V2", 
    "RMAIN/A2N", "RMAIN/A2N/Big Jonny", "RMAIN/A2N/Mizzen SS V2", 
    "RMAIN/A2N/Spi SS v2/Big Jonny", "RMAIN/A4", "RMAIN/A4/Big Jonny", 
    "RMAIN/A4/Big Jonny/Mizzen SS V2", "RMAIN/A4/CMiz SS", "RMAIN/A4/J2 N/Big Jonny", 
    "RMAIN/A4/Mizzen SS V2", "RMAIN/A4/Spi SS v2/Big Jonny", 
    "RMAIN/J1 N", "RMAIN/J1 N/Black Betty v2", "RMAIN/J1 N/CMiz SS", 
    "RMAIN/J1 N/Mizzen SS V2", "RMAIN/J2 N", "RMAIN/J2 N/Black Betty v2", 
    "RMAIN/J2 N/CMiz SS", "RMAIN/J2 N/Mizzen SS V2"), class = "factor"), 
    pd.TWA = c(40.9, 38.8, 37.8, 37.3, 45.2, 50.6), pd.TWS = c(13.7, 
    13, 13.3, 13.3, 13.2, 13.2)), row.names = c(NA, 6L), class = "data.frame")

Upvotes: 0

Views: 42

Answers (1)

Roman Luštrik
Roman Luštrik

Reputation: 70643

You can use ifelse to do this.

iris$sns <- ifelse(iris$Species == "setosa", "setosa", "notsetosa")

set.seed(357)
iris <- iris[sample(1:nrow(iris)),]

> head(iris[sample(1:nrow(iris)),])
   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species       sns
17          5.4         3.9          1.3         0.4     setosa    setosa
9           4.4         2.9          1.4         0.2     setosa    setosa
42          4.5         2.3          1.3         0.3     setosa    setosa
34          5.5         4.2          1.4         0.2     setosa    setosa
96          5.7         3.0          4.2         1.2 versicolor notsetosa
68          5.8         2.7          4.1         1.0 versicolor notsetosa

Upvotes: 1

Related Questions