我正在尝试计算相对于数据框所有列的基线值的降水变化百分比。
这是我的数据样本:
df <- structure(list(Year = c(2011, 2012, 2013, 2014, 2015, 2016, 2017,
2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028,
2029, 2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038, 2039,
2040), CanESM2 = c(1062.28463487013, 1081.79875965293, 954.918023984367,
1091.10825280531, 1015.6686025322, 1004.68487579451, 900.568853780764,
944.304316538189, 1131.29811907252, 880.640168198752, 1036.79227239276,
1104.19653571418, 1016.11848396336, 964.309534825316, 995.667089383537,
949.590669553214, 1031.46345256665, 950.988527516706, 997.957590593111,
1021.01557120052, 1014.29734436525, 1100.42743380135, 990.129571074739,
988.028837921423, 852.09126335109, 984.607515553816, 994.858568349016,
924.28298500481, 1030.62630719001, 971.449059906356), `GFDL-ESM2M` = c(939.797764384418,
862.438744527484, 833.258752796628, 694.001972600954, 939.25225566724,
788.443578230132, 769.624653964961, 922.180523767384, 815.569903819933,
923.2684778161, 973.312551235934, 1000.87827680745, 722.100345996542,
754.896724902162, 946.691351890564, 1108.37198079835, 776.359064574635,
877.332713415863, 641.223755372774, 891.215662326288, 840.29228301442,
809.219713604778, 890.788503113143, 816.101567557099, 700.315931425182,
974.495291193691, 774.889934251068, 687.874812729862, 890.626164086368,
925.821083978776), `MRI-CGCM3` = c(963.714630100705, 1039.90725638013,
986.808066989304, 952.912003762131, 1038.96522172, 1097.12269863514,
1152.25870240938, 1039.16198171388, 1126.11992922617, 1113.46311136123,
1097.59899557621, 1133.00511613898, 1060.2239913503, 1080.25928340702,
999.790532217113, 1111.90775837155, 1103.01247472501, 1122.86866008689,
966.542898921792, 1121.08812228037, 1126.70968803791, 1237.48721073746,
1120.73913534847, 1065.51575339606, 1120.41655349731, 1056.45756230661,
978.781090360169, 1215.56226961328, 1100.79315092804, 1187.63969015419
), `NorESM1-M` = c(1035.9629164267, 1127.66908751496, 1145.04203724642,
1073.79587400944, 1108.66027421689, 1081.31321431081, 991.997929494315,
970.820426144731, 900.132816323447, 993.967016211343, 1093.47915064086,
1062.25356500083, 1009.98847866058, 1047.23083263362, 1006.76468700444,
953.814034566967, 1040.03729133431, 1015.64394700637, 971.068915900834,
1019.70349949653, 967.3858486578, 1012.08374091682, 1024.64494944056,
1008.14540593558, 1036.2929410322, 948.484238020871, 991.503419342391,
1008.67344263059, 989.210959828228, 1024.22494449965), inmcm4 = c(1156.30514792346,
978.782707800559, 800.222094903299, 936.254681298492, 876.693978852088,
897.739287157671, 909.653028295674, 904.362882202918, 804.087073124877,
910.965404178024, 852.324516060155, 823.608663016503, 793.372757614206,
935.721565622802, 910.427470364702, 831.026191632682, 912.055279276787,
953.783713646985, 1003.06614105855, 915.388398109226, 1002.55859526363,
882.401185901887, 936.531046202423, 952.577307893596, 858.244066168409,
962.797113287339, 816.732411218346, 769.801340383127, 758.534824983789,
931.961373442904)), .Names = c("Year", "CanESM2", "GFDL-ESM2M",
"MRI-CGCM3", "NorESM1-M", "inmcm4"), row.names = 31:60, class = "data.frame")
如您所见,我有五种不同的年降水量观测值。
对于每年(行)和列,如何计算相对于某个参考值的百分比变化,例如1100mm?
如果我要编写一个函数,它将类似于:
(x-1100)/1100
其中x
是数据框的每个元素,不包括第一列。
有什么想法吗?
答案 0 :(得分:2)
这样的事可能吗?
(df[,-1] -1100)/1100
最终实施取决于您想要的输出,例如是否要
覆盖你的df
列 df[,-1] <- (df[,-1] -1100)/1100
创建一个新的data.frame
new.df <- cbind( df[,1], (df[,-1] -1100)/1100 )
如果不是,您正在寻找什么,将样本数据减少到几年/行可能有助于其他人回答。