0

So I am trying to find the number of occurrences of each name in another dataset. The code I am trying to run is:

Data$Count <- grep(Data$Name,OtherDataSet$LeadName) %>% length()

The issue is when I run this, the number for the first name gets mapped to each spot in that column. Why is this happening?

Roland
  • 127,288
  • 10
  • 191
  • 288
  • `pattern` is not vectorized in `grep` Try `library(stringr); library(dplyr);Data %>% mutate(Count = sum(str_detect(OtherDataSet$LeadName, Name)))` assuming that 'Data' and 'OtherDataSet' have the same number of rows – akrun Oct 24 '18 at 03:43
  • I am iterating over a column in `LeadName` it does not work because they have different number of rows – WizardCovfefe Oct 24 '18 at 03:49
  • In that case, you may need `grep(paste0("\\b(", paste(Data$Name, collapse="|"), ")\\b"), OtherDataSet$LeadName)` – akrun Oct 24 '18 at 03:55
  • what does this do? – WizardCovfefe Oct 24 '18 at 04:04
  • `akrun` has made a pattern that uses the regex alternative symbol `|` to check if any of the options in `Data$Name` appear in `LeadName` – Calum You Oct 24 '18 at 05:05
  • I am trying to iterate through a column to see if each name in that column appears in another column, when I ran this, it did not work. Ideally I would like to be able to get a count of each appearance and map it to a dataframe – WizardCovfefe Oct 24 '18 at 05:10
  • Try providing some reproducible data? `library(tidyverse); Data %>% mutate(count = Name %in% OtherDataSet$LeadName)`? perhaps? But we cannot just guess at what is going wrong if you don't show us the actual situation; see how to make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Calum You Oct 24 '18 at 05:14

1 Answers1

0
library(tidyverse)
Data <- data_frame(Name=c("Dog","Cat","Bird"))
OtherDataSet <- data_frame(LeadName=c("Frog","Cat","Catfish","BirdOfPrey","Bird","Bird"))
Data <- Data %>% mutate(Count=map(.x = Name,~str_detect(.,pattern = OtherDataSet$LeadName)) %>% map_int(~sum(.)))
Edvardoss
  • 393
  • 3
  • 8