confusedfx - 15 days ago 5
R Question

# Running through a dataframe with sapply in R

``````m = data.frame(c(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10),
c("Lozenge","A","Lozenge","A","Lozenge","A","Lozenge","A","Lozenge","A","Lozenge","A","Lozenge","A","Lozenge","A","Lozenge","A","Lozenge","A"),
c("Lozenge","Lozenge","Lozenge","Lozenge","Lozenge","A","A","A","A","A"))

mm = data.frame(unique(m[,1]), 0, 0)
colnames(m) = c("Number","Old","New")
colnames(mm) = c("Number","Old","New")
``````

I currently have two different dataframes, and I want to use an sapply to go through the m dataframe. Using mm's Number, I want to take a look to see if it matches m's Number. If it does, it will take a look through and see if the word "Lozenge" appears in the Old column and/or the New Column. If it does, I want to +1 in the respective column in mm. I've been playing around with it, but I can't wrap my head around it.

This seems less like an `apply` problem and more just a manipulation and summary problem.

Do you really need the separate placeholder `mm` dataframe? If you do, you can set the results of this chain to mm.

``````library(dplyr)
library(tidyr)

m %>%
gather(condition, value, Old, New) %>%
filter(value == "Lozenge") %>%
group_by(Number, condition) %>%
tally %>%

Number   New   Old
(dbl) (dbl) (dbl)
1       1     2     1
2       2     2     1
3       3     1     1
4       4     0     1
5       5     0     1
6       6     2     1
7       7     2     1
8       8     1     1
9       9     0     1
10     10     0     1
``````
Source (Stackoverflow)