-2

Frustrated newbie question in R:

Say I have a list of strings = ("a", "b", "c"), and a data frame with a column df$stuff.

I want to loop through each string in the list, count the number of times that string appears in df$stuff, and add it cumulatively. In other words, the number of times "a" appears, plus the number of times "b" appears, plus the number of times "c" appears. I've tried count, table, and aggregate functions, and all I get is errors.

There simply has to be a nice clean way of doing this.

TPL
  • 21
  • 3
  • Welcome to Stack Overflow! I will answer your question to the best of my ability now, but in order for us to be able to answer your question, please include a sample of your data by typing `dput(variableName)` and copying and pasting the console output into your question in the future. For more information on how to make a reproducible example in `R` (and make it more likely your question is answered) please view [this post](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Barker Dec 07 '16 at 01:54

2 Answers2

0

It is difficult to answer this without a sample of your data and what you want the output to look like, but I will try. First I will make a guess at what your data look like:

df <- data.frame(stuff = sample(letters[1:5], 30, replace = TRUE))
strings <- letters[1:3]

To get the counts of strings in df[["stuff"]] you can use table and then index into the table with strings.

table(df[["stuff"]])[strings]
Barker
  • 2,074
  • 2
  • 17
  • 31
0

I had a different idea about what was being asked. So I will give it a shot too.

strings = c("a", "b", "c")
stuff = c("the cat", "the bat", "the dog")
sapply(strings, function(s) length(grep(s, stuff)))
a b c 
2 1 1

Gets you the number of matches for each string. So

sum(sapply(strings, function(s) length(grep(s, stuff))))

gives you the sum of all of those.

Is that what you wanted?

G5W
  • 36,531
  • 10
  • 47
  • 80
  • Thanks, guys. Your comments helped me figure it out. When I used the table function on my dataset, I was very confused by the fact that result contains the descriptive string along with the value. For example, it isn't just the value 6, it is "Some string I hate 6". The double square brackets notation let me pull the value out of the table. – TPL Dec 08 '16 at 21:53
  • 1
    @TPL if you figured out a solution, you should post it as an answer so others with the same problem can be helped by it. – Barker Dec 08 '16 at 22:02