Reputation: 463
I currently have this code written to count how many times the first letter of a code appears in a specific column in a table.
#a test data frame
test <- data.frame("State" = c("PA", "RI", "SC"), "Code1" = c("EFGG, AFGG", "SSAG", "AFGG, SSAG"))
#code to count method codes
test[] <- lapply(test, as.character)
test_counts <- sapply(strsplit(test$Code1, ",\\s+"), function(x) {
tab <- table(substr(x, 1, 1)) # Create a table of the first letters
paste0(names(tab), tab, collapse = ", ") # Paste together the letter w/ the number and collapse
them
} )
#example of output
[1] "A1, E1" "S1" "A1, S1"
Everything about this current code is perfect, except I would like R to not output the counts in alphabetical order. I would like it to preserve the order of codes. So this is how I would like the output to look:
[1] "E1, A1", "S1", "A1, S1"
Thank you!!
Upvotes: 1
Views: 164
Reputation: 887911
Another option with tidyverse
. We could split the 'Code1' with separate_rows
, get the count
and do a group_by
paste
after arrange
ing the rows based on the frequency column
library(dplyr)
library(tidyr)
test %>%
separate_rows(Code1) %>%
mutate(Code1 = substr(Code1, 1, 1)) %>%
count(State, Code1) %>%
arrange(State, n) %>%
unite(Code1, Code1, n, sep="") %>%
group_by(State) %>%
summarise(Code1 = toString(Code1), .groups = 'drop') %>%
pull(Code1)
#[1] "A1, E1" "S1" "A1, S1"
Upvotes: 0
Reputation: 102740
Here is a base R option using factor
to address the issue
sapply(
strsplit(test$Code1, ", "),
function(x) {
toString(
do.call(
paste0,
rev(stack(table(factor(u<-substr(x, 1, 1),levels = unique(u)))))
)
)
}
)
which gives
[1] "E1, A1" "S1" "A1, S1"
Upvotes: 1