Let's say I have the following dataframe:
X1 X2
Fox A/B/C/D
Duck A/B/E/F
Bird A/E/G
And I want to count how many times the values in X2 shows up (without manually entering the letters to count).
The result would be:
X1 X2 (counts)
A 3
B 2
C 1
D 1
E 2
F 1
G 1
I have no idea on how to begin with this :(
CodePudding user response:
Split the column at the /
, get the frequency count with table
and stack
to a two column data.frame
in base R
stack(table(unlist(strsplit(df1$X2, "/", fixed = TRUE))))[2:1]
-output
ind values
1 A 3
2 B 2
3 C 1
4 D 1
5 E 2
6 F 1
7 G 1
data
df1 <- structure(list(X1 = c("Fox", "Duck", "Bird"), X2 = c("A/B/C/D",
"A/B/E/F", "A/E/G")), class = "data.frame", row.names = c(NA,
-3L))