简体   繁体   English

计算两个日期的两个值之间的差异?

[英]Comput the difference between two values for two dates?

In this data, i would like to compute the difference between the 2 values of ax (and all columns) that correspond to 1990 and 1999.在此数据中,我想计算对应于 1990 年和 1999 年的两个 ax 值(和所有列)之间的差异。

myd=structure(list(year = c(1990, 1991, 19962, 1993, 1994, 1999), 
ud = c(137.25, 135, 125, 139, 
125.5, 127.5), ax = c(67, 70, 
69, 71, 69, 68
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))

Then plot the results using ggplots as bars然后 plot 结果使用 ggplots 作为条形图

The solution has two parts:解决方案有两个部分:

  • wrangle the dataset so that it is tidy for your purposes整理数据集,使其对您的目的保持整洁
  • plot the graph plot 图

Wrangling the data is straightforward.整理数据很简单。 (The code here could be shortened, but I've written it as it is to make the various steps clear.) Also note the "obvious" correction to the typo mentioned by @user2974951. (此处的代码可以缩短,但我已经按原样编写了它以使各个步骤清晰。)还要注意对@user2974951 提到的错字的“明显”更正。

# Extract the baseline values and convert to long format
baseline <- myd %>% 
              filter(year == 1990) %>% 
              select(-year) %>% 
              pivot_longer(everything(), names_to="variable", values_to="baseline") 
# Extract the endpoint values and convert to long format
endpoint <- myd %>% 
              filter(year == 1999) %>% 
              select(-year) %>% 
              pivot_longer(everything(), names_to="variable", values_to="endpoint")
# Merge by variable and calculate difference
difference <- baseline %>% 
                full_join(endpoint, by="variable") %>% 
                mutate(diff=endpoint-baseline)

At this point, difference looks like this:此时, difference如下所示:

> difference
# A tibble: 2 × 4
  variable baseline endpoint  diff
  <chr>       <dbl>    <dbl> <dbl>
1 ud           137.     128. -9.75
2 ax            67       68   1   

Now create the bar chart.现在创建条形图。

# Create the bar chart
difference %>% 
  ggplot() +
  geom_col(aes(x=variable, y=diff))

在此处输入图像描述

Note that this solution is robust with respect to the number of variables in the original dataset, and their names.请注意,该解决方案在原始数据集中的变量数量及其名称方面是稳健的。 It will also handle missing values without error.它还将处理缺失值而不会出错。 It could easily be generalised to calculate and plot the difference between any two years (eg earliest year as baseline and most recent as endpoint).它可以很容易地推广到计算和 plot 任何两年之间的差异(例如,最早的年份作为基线,最近的年份作为终点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM