My goal: count the number of times "A" appears in df
- it will always be in col1 or col2 (never on the same row).
Then, loop over and repeat for all other letters.
library(reshape2)
#Make a dataframe with just two columns of characters
df <- data.frame("col1"= c("A", "B", "C", "D", "E", "F", "G", "A"), "col2"=c("Q", "A", "S", "Z", "A", "C", "F", "X"))
df
col1 col2
1 A Q
2 B A
3 C S
4 D Z
5 E A
6 F C
7 G F
8 A X
I made this loop, which seems to work for me. The process is: stack col1
and col2
on top of each other, get the unique characters, loop over df
to see how many times each character is found, then store the results in newdf:
newdf = NULL
#For any unique characters between col1 and col2, count how many times they appear in df
for(i in unique(stack(df)$value)){
newdf <- rbind(newdf,data.frame("unique_char"=i, "number_hits"=nrow(df[df[1]==i | df[2]==i,])))
}
Result looks good:
newdf
unique_char number_hits
1 A 4
2 B 1
3 C 2
4 D 1
5 E 1
6 F 2
7 G 1
8 Q 1
9 S 1
10 Z 1
11 X 1
My question: How can I insert this loop into a function so that I can perform this on any dataframe? I tried the following...
myfun <- function(a){
newdf = NULL
#For any unique characters between col1 and col2, count how many times they appear in df
for(i in unique(stack(a)$value)){
newdf <- rbind(newdf,data.frame("unique_char"=i, "number_hits"=nrow(a[a[1]==i | a[2]==i,])))
}
}
But it doens't work. If I run myfun(df)
nothing happens.