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

在R语言中基于名义值计算实际美元值的技术咨询

Converting Nominal to Real Dollars for Plan A in R

Got it, let's tackle this. First, let's make sure we're working with the exact dataset you provided:

# Your original dataset
Plan <- c("A","A","A","B","B","B","B")
Plan_Period <- c(1,2,3,1,2,3,4)
Plan_Elapsed_time <- c(0.5,1,0.25,1,0.5,0.3,0.25)
year <- c(2016,2017,2018,2015,2016,2017,2018)
Inflation <- c(1.014,1.012,1.012,1.013,1.012, 1.080,1.020)
Cost <- c(10,20,30,40,40,50,60)
data <- data.frame(Plan, Plan_Period, Plan_Elapsed_time, year, Inflation, Cost)

Since your formula for real dollars was cut off, I'll cover the two most common inflation scenarios you're likely dealing with, using tidyverse tools (dplyr) because they make grouping/filtering data straightforward.


Scenario 1: Inflation is a base-year relative index

If your Inflation column represents how much more expensive each year is compared to a fixed base year (e.g., 2016 = 1.014 means 2016 prices are 1.4% above the base), the formula for real dollars (converted to your chosen base year) is:

Real Cost = Nominal Cost × (Base Year Inflation Index / Current Year Inflation Index)

Let's use Plan A's first year (2016) as our base year. Here's how to implement this:

library(dplyr)

# Set your desired base year (adjust this to whatever you need)
base_year <- 2016

# Calculate real costs only for Plan A
data_with_real_costs <- data %>%
  group_by(Plan) %>%
  mutate(
    Real_Cost = if_else(Plan == "A",
                       Cost * (Inflation[year == base_year] / Inflation),
                       NA_real_)  # Leave Plan B as NA or adjust if needed
  ) %>%
  ungroup()

# Check the results
print(data_with_real_costs)

Scenario 2: Inflation is a year-over-year (环比) growth rate

If Inflation is a year-over-year increase (e.g., 2016's 1.014 means prices rose 1.4% from 2015), we first need to calculate a cumulative index relative to our base year, then convert nominal costs:

library(dplyr)
library(tidyr)

base_year <- 2016

# First, build a cumulative inflation index relative to the base year
inflation_base_index <- data %>%
  distinct(year, Inflation) %>%
  arrange(year) %>%
  mutate(
    # Calculate cumulative inflation from the earliest year
    cumulative_index = cumprod(Inflation),
    # Normalize to the base year so base year = 1
    base_relative_index = cumulative_index / cumulative_index[year == base_year]
  )

# Merge back to original data and compute real costs
data_with_real_costs <- data %>%
  left_join(inflation_base_index, by = "year") %>%
  mutate(
    Real_Cost = if_else(Plan == "A",
                       Cost / base_relative_index,  # Divide by index to get real value
                       NA_real_)
  ) %>%
  # Clean up temporary columns
  select(-cumulative_index, -base_relative_index)

# View results
print(data_with_real_costs)

Quick Notes

  • If you want to convert Plan B's costs too, just remove the if_else condition and calculate Real_Cost for all rows.
  • Adjust base_year to any year in your dataset (e.g., 2018) to convert to that year's dollars.
  • Double-check your inflation definition to pick the right scenario—if your original formula had specific logic, let me know and I can tweak this!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:32:28