I have a dataframe with 1.8 million rows, and each row contains one or more of a selection of 30,000 tags. I'm trying to find how many rows contain an instance of each tag. Some items contain as many as 25 tags, each of which is in its own column. No row contains more than one instance of any tag:

```
ItemNo <- c(1, 2, 3, 4)  
Tag1 <- c("ZZZ", "AAA", "BBB", "YYY")  
Tag2 <- c("YYY2", "ZZZ", "AAA", "XXX")  
Tag3 <- c("", "YYY2", "AAA2", "XXX3")  
Tag4 <- c("", "", "", "AAA")  
Tag5 <- c("", "", "", "ZZZ")  
Tag6 <- c("", "", "", "YYY2")  
Items <- data.frame(ItemNo, Tag1, Tag2, Tag3, Tag4, Tag5, Tag6)  
Items

  ItemNo Tag1 Tag2 Tag3 Tag4 Tag5 Tag6
1      1  ZZZ YYY2       
2      2  AAA  ZZZ YYY2   
3      3  BBB  AAA AAA2   
4      4  YYY  XXX XXX3  AAA  ZZZ YYY2

```

The universe of tags is in a separate data set:

```
Code <- c("AAA", "BBB", "XXX", "ZZZ", "AAAA", "XXX3", "YYY2")  
COUNT <- c(0, 0, 0, 0, 0, 0, 0)  
tags <- data.frame(Code, COUNT) 
tags 

  Code COUNT  
1  AAA     0  
2  BBB     0  
3  XXX     0  
4  ZZZ     0  
5 AAAA     0  
6 XXX3     0  
7 YYY2     0  

```

I want to end up with something like this: 

```
   Code COUNT  
1  AAA     3  
2  BBB     1  
3  XXX     1  
4  ZZZ     3  
5 AAAA     0  
6 XXX3     1  
7 YYY2     3   

```

I'm able to get good results with a for loop, which takes about 3 hours to run through the dataset:

```
for (i in 1:nrow(tags)) {tags[i,2] <- sum(Items[,2:7] ==
as.character(tags[i,1]), na.rm = TRUE)}

```

Is there a more efficient or elegant way to count the instances of each tag in this data set?