You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于reshape2库按薪资统计量分组分析含性别婚姻的CSV数据

Solution Using reshape2 for Wage Summary Stats by Gender & Marital Status

Got it, let's walk through how to get the wage statistics you need while grouping by gender (and focusing on male marital status) using reshape2—plus a few helper packages to make the stats calculation smooth.

Step 1: Load Required Libraries & Import Data

First, we'll need reshape2 for data reshaping, dplyr for easy grouping, and moments to calculate kurtosis (since base R doesn't have a built-in function for that):

# Load necessary packages
library(reshape2)
library(dplyr)
library(moments)

# Import your CSV file (replace with your actual file path/name)
wage_data <- read.csv("your_wage_data.csv")

Step 2: Calculate Grouped Summary Statistics

We'll group the data by gender and marital status, then compute the four stats you mentioned: average (mean), median, kurtosis, and standard deviation. We'll handle missing values with na.rm = TRUE to avoid errors:

# Compute summary stats grouped by gender and marital status
grouped_stats <- wage_data %>%
  group_by(gender, status) %>%
  summarise(
    mean_wages = mean(wages, na.rm = TRUE),
    median_wages = median(wages, na.rm = TRUE),
    kurtosis_wages = kurtosis(wages, na.rm = TRUE),
    sd_wages = sd(wages, na.rm = TRUE)
  ) %>%
  ungroup()

Step 3: Use reshape2 to Reshape Data for Sharing

If you need to restructure the data (e.g., switch between wide/long formats for easier sharing or visualization), reshape2's melt() and dcast() functions come in handy.

Example 1: Convert to Long Format

This makes it easy to view all stats in a single column, grouped by gender and status:

# Melt wide stats into long format
melted_stats <- melt(grouped_stats, 
                     id.vars = c("gender", "status"),
                     variable.name = "statistic_type",
                     value.name = "wages_value")

Example 2: Filter for Male-Only Records

To focus specifically on male marital status groups, filter the results:

# Get stats only for male respondents
male_only_stats <- grouped_stats %>%
  filter(gender == "M")

Example Output Preview

Using your sample data, the grouped_stats dataframe would look something like this:

genderstatusmean_wagesmedian_wageskurtosis_wagessd_wages
MNO5833.336500[calculated value]2020.73
MYes30003000[calculated value]0
WNO62506250[calculated value]2474.87
WYes35003500[calculated value]0

Note: Kurtosis values depend on the full dataset, but the structure will match above.

内容的提问来源于stack exchange,提问作者Dany worner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:08:47