如何使用R查找多个列中比率的差异

时间:2018-10-11 13:49:56

标签: r diff

我有4个不同年份的数据框,带有值。我需要找出这些价值在所有年份中如何变化,即哪个城市过于频繁地改变其价值,哪一个是最小的。

City     Ratio1     Ratio2     Ratio3     Ratio4
A        1.0177722  1.0173251  1.0133026  1.0140027
B        1.0132619  1.0122653  1.0128473  1.0111068
C        1.0689484  1.0640355  1.0625305  1.0544790

..... other 1000 entries

我试图通过区别来做到这一点,但是没有运气。问题是哪个城市的比率在ratio1到ratio4之间变化最大,而变化最小。 我曾尝试使用mutate函数来计算方差,但这会向我发出警告:

DF<- DF%>% mutate(vari = var(Ratio1:Ratio4,na.rm = T))

Warning messages:
1: In POP_2013_ratio:POP_2016_ratio :
  numerical expression has 439 elements: only the first used
2: In POP_2013_ratio:POP_2016_ratio :
  numerical expression has 439 elements: only the first used

1 个答案:

答案 0 :(得分:0)

R的data.table包提供了一种非常简洁的方法来基于现有列创建新列:

dt <- data.table(City = c("A", "B", "C"),
                 Ratio1 = c(1.0177722, 1.0132619, 1.0689484),
                 Ratio2 = c(1.0173251, 1.0122653, 1.0640355), 
                 Ratio3 = c(1.0133026, 1.0128473, 1.0625305), 
                 Ratio4 = c(1.0140027,1.0111068, 1.0544790))
>dt 
   City   Ratio1   Ratio2   Ratio3   Ratio4
1:    A 1.017772 1.017325 1.013303 1.014003
2:    B 1.013262 1.012265 1.012847 1.011107
3:    C 1.068948 1.064035 1.062531 1.054479

您可以试用一些功能,然后查看最适合的功能:

 dt[, diff := Ratio4-Ratio1
    ][, abs_diff := abs(Ratio4-Ratio1)
      ][, range:= max(c(Ratio1, Ratio2, Ratio3, Ratio4))-  min(c(Ratio1, Ratio2, Ratio3, Ratio4)), by = City
        ][,variance:=var(c(Ratio1, Ratio2, Ratio3, Ratio4)), by = City]

 >dt

    City   Ratio1   Ratio2   Ratio3   Ratio4       diff  abs_diff     range     variance
 1:    A 1.017772 1.017325 1.013303 1.014003 -0.0037695 0.0037695 0.0044696 5.174612e-06
 2:    B 1.013262 1.012265 1.012847 1.011107 -0.0021551 0.0021551 0.0021551 8.766456e-07
 3:    C 1.068948 1.064035 1.062531 1.054479 -0.0144694 0.0144694 0.0144694 3.609233e-05

当您最终决定要使用的条件(比如方差)时,您可以使用以下方法选择排名靠前的城市:

dt[order(-variance)][1]

>dt
   City   Ratio1   Ratio2   Ratio3   Ratio4       diff  abs_diff     range     variance
1:    C 1.068948 1.064035 1.062531 1.054479 -0.0144694 0.0144694 0.0144694 3.609233e-05