Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8211769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:32:24+00:00 2026-06-07T10:32:24+00:00

I have a data frame with two columns, and want to create a third

  • 0

I have a data frame with two columns, and want to create a third column which will essentially be a boolean for whether or not column two contain a certain set of specified values.

f <- data.frame(name=c("John", "Sara", "David", "Chad"),
                 car=c("Honda|Ford", "BMW", "Toyota|Chevy|Ford", 
                 "Toyota|Chevy|Ford|Honda"))

The first thing I did was remove the | from each string in the second column, and placed those valued in a third column

library(stringr)
g = str_replace_all(f$car, "[^[:alnum:]]", " ")
f$make = c(g)
f

What I want to do now if create another column, which will be a boolean, 1 if make contains a common car, and 0 if it contains a not common car.

common = c("Honda", "Ford", "Toyota", "Chevy")
not_common = c("BMW", "Lexus", "Acura")

I’ve tried a few things, including the stringr package and ifelse to produce the following output.

   name                     car                    make       common   
1  John              Honda|Ford              Honda Ford           1
2  Sara                     BMW                     BMW           0
3 David       Toyota|Chevy|Ford       Toyota Chevy Ford           1
4  Chad Toyota|Chevy|Ford|Honda Toyota Chevy Ford Honda           1

Since it’s possible to have both a common and uncommon car as an entry, the uncommon make should override the common make and that row should take the value 0 in the common column. So if an entry had both BMW and Ford, that entry should take a 0 in the common column.

Can anyone help with this task.

Oh, and here’s what I tried with the stringr package, but it doesn’t work.

common = c("Honda", "Ford", "Toyota", "Chevy")
not_common = c("BMW", "Lexus", "Acura")
common_match <- str_c(common)
not_match <- str_c(not_common)

main <- function(df) {
  f$new_make <- str_detect(f$make, common_match)
  df
}

main(f)

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:32:25+00:00Added an answer on June 7, 2026 at 10:32 am

    Another way and a comparison

    f2 <- f[rep(1:4,50000),]
    system.time({
    v <- sapply(f2$make, strsplit, " ")
    sapply(v, function(x) max(1-not_common %in% x)*max(common %in% x))
    })
     user  system elapsed 
     7.94    0.01    8.00 
    
    system.time(sapply(f2$car,function(x) ifelse(length(grep("BMW|Lexus|Acura",x))>0,0,1)))
     user  system elapsed 
    28.72    0.04   28.87 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame with two columns. First column contains categories such as
I have data frame with two column say a and b now I want
I have a data frame with two columns. The first column defines subsets of
I have a state column in a dataframe and I want to create two
I have two columns in a data frame, and I have been able to
I have two data frames with two columns each. The first column is timestamps
I have a data frame with two qualitative variables (Q1, Q2) which are both
I have a data frame the first columns of which are a sample ID
I have a data.frame in R. I want to try two different conditions on
I am trying create a data.frame from which to create a graph. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.