kirellos said
kirellos said

Reputation: 21

Frequency across multiple variables

My data have more than 50 variables and the same values distributed across them. I need to table each value (total values more than 1000 ) with its frequency across the 40 variables.

I need to create a table for each values across all variables for example F447 5 A257 4 G1229 5 C245 2

enter image description here

Upvotes: 1

Views: 164

Answers (1)

AndrewGB
AndrewGB

Reputation: 16876

You can use table to get the frequency across the variables, then we can convert to a dataframe and make the variable names a column, and finally arrange by frequency. If you want to put in descending order, then we could do arrange(desc(frequency)).

library(dplyr)

data.frame(frequency = unclass(table(unlist(df)))) %>% 
  tibble::rownames_to_column("variable") %>% 
  arrange(frequency)

Output

  variable frequency
1     C245         1
2     A257         2
3     F447         3
4    G1229         3

Or with base R, you could do:

results <- data.frame(frequency = unclass(table(unlist(df))))
results$variable <- row.names(results)
row.names(results) <- NULL
results <- results[order(results$frequency),c(2,1)]

Another option if you would like additional summary and visualization of the frequency, then epiDisplay is a good option.

library(epiDisplay)

tab1(unlist(df), sort.group = "decreasing", cum.percent = TRUE)

Output

        Frequency Percent Cum. percent
G1229           3    33.3         33.3
F447            3    33.3         66.7
A257            2    22.2         88.9
C245            1    11.1        100.0
  Total         9   100.0        100.0

enter image description here

Data

df <- structure(list(Var1 = c("F447", "A257", "G1229"), Var2 = c("G1229", 
"F447", "A257"), Var3 = c("C245", "F447", "G1229")), class = "data.frame", row.names = c(NA, 
-3L))

Upvotes: 2

Related Questions