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

R语言中统计DataFrame各国籍对应男女人数的方法求助

Answer

Hey there! As someone with solid JavaScript experience who's just starting out with R, I totally get wanting to translate those data handling skills over. Let's break down your two requests step by step.

1. Count Male/Female per Nationality in Your DataFrame

First, let's recreate the DataFrame you provided (I cleaned up the formatting a bit for clarity):

# Construct your sample DataFrame
df <- data.frame(
  Gender = c("Male", "Male", "Female", "Male", "Female"),
  Nationality = c("American", "American", "American", "French", "Spanish")
)

There are two straightforward ways to get the gender counts per nationality, depending on what output format you prefer:

Option 1: Base R's table() function (quick cross-tab)

This is the simplest method for a fast overview—great if you just need a quick snapshot:

# Generate a cross-tab of Nationality vs Gender
gender_nationality_counts <- table(df$Nationality, df$Gender)
print(gender_nationality_counts)

Output:

Female Male
  American     1    2
  French       0    1
  Spanish      1    0

Option 2: dplyr for structured DataFrame output (flexible)

If you want the result as a tidy DataFrame (similar to how you might work with objects/arrays in JS), the dplyr package is perfect. Its chainable syntax (%>%) feels intuitive if you're used to JS method chaining:

# Install dplyr first if you haven't: install.packages("dplyr")
library(dplyr)

# Group, count, and reshape to wide format
gender_nationality_summary <- df %>%
  group_by(Nationality, Gender) %>%
  summarise(Count = n(), .groups = "drop") %>%
  tidyr::pivot_wider(names_from = Gender, values_from = Count, values_fill = 0)

print(gender_nationality_summary)

Output:

# A tibble: 3 × 3
  Nationality Female  Male
  <chr>        <int> <int>
1 American         1     2
2 French           0     1
3 Spanish          1     0

2. Beginner Resources for String-Focused DataFrame Analysis

Since you're working with string-heavy DataFrames, here are tailored resources to get you up to speed:

  • Base R Built-in Help: Start with core documentation. Type ?data.frame to learn DataFrame basics, ?table for cross-tabulation, and once installed, ?stringr for string operations—all have practical examples.
  • R for Data Science (Book): The Data Transformation and Strings chapters are essential. They use dplyr (for DataFrames) and stringr (for strings) with real-world examples, covering everything from grouping data to pattern matching in strings.
  • stringr Official Vignette: This deep dive into the stringr package is full of actionable code for cleaning and analyzing string columns. Run vignette("stringr") in your R console to access it.
  • dplyr Getting Started Guide: The dplyr vignette (vignette("dplyr")) walks you through filtering, grouping, and summarizing DataFrames—skills you’ll use daily. Its chainable syntax will feel familiar if you’re used to JS method chaining.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:29:01