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

RStudio新手求助:多条件创建新列失败,需绘制分时段箱线图

Hey Daniel, let's sort out that period column issue first, then get you set up for those boxplots!

What's Wrong with Your Current Code?

Looking at your ifelse line, the problem is likely two-fold:

  1. You forgot to prefix Anyo with your data frame name a$ in the second condition. Without that, R can't locate the column, so the second condition never evaluates to TRUE—meaning any row that doesn't fit the first condition gets dumped straight into the "80s" group.
  2. You mentioned your dataset has a year column, but your code uses Anyo. Double-check your actual column name—if it's year instead of Anyo, that's another reason your logic isn't working as expected.

Fixed ifelse Code

First, confirm your year column name. If it's year, use this:

a$period <- ifelse(a$year >= 2011 & a$year <= 2040, "20s",
                   ifelse(a$year >= 2041 & a$year <= 2070, "50s", "80s"))

If your column is indeed named Anyo, just fix the missing a$ in the second condition:

a$period <- ifelse(a$Anyo >= 2011 & a$Anyo <= 2040, "20s",
                   ifelse(a$Anyo >= 2041 & a$Anyo <= 2070, "50s", "80s"))

A Cleaner Alternative: case_when (from dplyr)

Nested ifelse statements get messy fast. Using dplyr::case_when makes your logic way more readable and less error-prone. First load the dplyr package:

library(dplyr)

# Update with your actual year column name (year or Anyo)
a <- a %>%
  mutate(period = case_when(
    year >= 2011 & year <= 2040 ~ "20s",
    year >= 2041 & year <= 2070 ~ "50s",
    year >= 2071 & year <= 2100 ~ "80s",
    TRUE ~ NA_character_  # Optional: catch any years outside your target range
  ))

Verify Your period Column

Before plotting, make sure the groups are correctly assigned with this quick check:

table(a$period)

This will show you how many rows fall into each "20s", "50s", "80s" group—you should see counts for all three if your dataset covers the full 2011-2100 range.

Plotting the Boxplots (with ggplot2)

Once your period column is set up, let's make those boxplots using ggplot2 for customizable, polished visuals.

Single Variable Example (e.g., sta1p)

library(ggplot2)

ggplot(a, aes(x = period, y = sta1p)) +
  geom_boxplot(fill = "lightsteelblue", alpha = 0.7) +
  labs(title = "Distribution of sta1p Across Time Periods",
       x = "Time Period",
       y = "sta1p Value") +
  theme_minimal()

Multiple Variables (All Station/Experiment Columns)

If you want to plot all your sta*p and sta*t columns at once, first reshape your data to long format with tidyr::pivot_longer:

library(tidyr)

a_long <- a %>%
  pivot_longer(
    cols = starts_with("sta"),  # Grab all columns starting with "sta"
    names_to = "station_experiment",
    values_to = "measurement"
  )

# Now plot all groups side-by-side
ggplot(a_long, aes(x = period, y = measurement, fill = station_experiment)) +
  geom_boxplot(alpha = 0.7, position = position_dodge(width = 0.8)) +
  labs(title = "Measurements by Time Period and Station/Experiment",
       x = "Time Period",
       y = "Measurement Value",
       fill = "Station & Experiment Type") +
  theme_minimal()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:21:13