在R语言中基于多变量生成结果统计表的技术问询
Great question! When dealing with multiple columns of the same type (like your PR1 to PR25 surgery codes), the key is to first reshape your data from wide to long format—this makes it easy to aggregate counts across all variables. Here's how you can do this in R using tidyverse tools, which are perfect for this kind of task:
Step 1: Reshape Wide Data to Long Format
First, we'll convert your wide data frame into a long format where each row represents a single surgery code entry from any PR column:
# Load required library library(tidyverse) # Replace "your_data" with your actual data frame name long_data <- your_data %>% pivot_longer( cols = starts_with("PR"), # Target all columns starting with "PR" (works for PR1-PR25) names_to = "PR_variable", # New column to store which PR column the code came from values_to = "surgery_code" # New column to store the surgery code itself )
Step 2: Generate the Summary Table
Now we can calculate key stats like total frequency, which PR columns each code appears in, and percentages:
summary_table <- long_data %>% group_by(surgery_code) %>% summarize( total_count = n(), # Total number of times the code appears across all PR columns unique_PR_columns = n_distinct(PR_variable), # How many different PR columns include this code PR_columns_list = paste(unique(PR_variable), collapse = ", "), # List of specific PR columns percentage_of_total = round((total_count / nrow(long_data)) * 100, 2) # Percentage of all entries ) %>% arrange(desc(total_count)) # Sort from most to least frequent codes
Example Output (Using Your Sample Data)
For your provided sample data, the resulting summary_table would look like this:
| surgery_code | total_count | unique_PR_columns | PR_columns_list | percentage_of_total |
|---|---|---|---|---|
| 222 | 3 | 3 | PR1, PR2, PR3 | 20.00 |
| 527 | 2 | 2 | PR1, PR2 | 13.33 |
| 569 | 2 | 3 | PR1, PR2, PR3 | 13.33 |
| 341 | 2 | 2 | PR1, PR3 | 13.33 |
| 1422 | 2 | 2 | PR2, PR3 | 13.33 |
| 1600 | 1 | 1 | PR1 | 6.67 |
| 1660 | 1 | 1 | PR3 | 6.67 |
Base R Alternative (No Tidyverse)
If you prefer not to use tidyverse, here's a base R approach:
# Reshape using stack() long_data_base <- stack(your_data[, grep("^PR", colnames(your_data))]) # Calculate frequency counts count_table <- as.data.frame(table(long_data_base$values)) colnames(count_table) <- c("surgery_code", "total_count") # Add percentage count_table$percentage_of_total <- round((count_table$total_count / nrow(long_data_base)) * 100, 2) # Sort by frequency count_table <- count_table[order(-count_table$total_count), ]
Both methods scale seamlessly to your full PR1-PR25 dataset—no need to list each column individually!
内容的提问来源于stack exchange,提问作者TimF

