0

I am a beginner in need of some guidance.

I have a data frame that looks like the following (simplified example w. two columns, was imported from a CSV):

Price | Features
100 | {TV, wifi}
125 | {TV, wifi, "Air conditioning"}
110 | {wifi, "Wheelchair accessible", Sauna}

What I would like to do in this example is create a new column called "TV". This column would indicate TRUE if TV is listed as a feature, and FALSE if TV is not listed.

Price | Features | TV
100 | {TV, wifi} | TRUE
125 | {TV, wifi, "Air conditioning"} | TRUE
110 | {wifi, "Wheelchair accessible", Elevator} | FALSE

I have googled and found examples of how to do this based on conditions (if x = TRUE or x > 0) but I'm unclear how to do it when I am looking for characters/phrases present in a variable.

0

1 Answer 1

0

You could try pattern matching like so with grepl:

tvs <- data.frame(
  Price = c(100, 125, 110),
  Features = c(
    '{TV, wifi}',
    '{TV, wifi, "Air conditioning"}',
    '{wifi, "Wheelchair accessible", Sauna}'
  ),
  stringsAsFactors = FALSE
)

tvs$TV <- grepl("TV", tvs$Features, fixed = TRUE)

So now tvs is:

  Price                               Features    TV
1   100                             {TV, wifi}  TRUE
2   125         {TV, wifi, "Air conditioning"}  TRUE
3   110 {wifi, "Wheelchair accessible", Sauna} FALSE
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.