我有以下数据集。
structure(list(Monate = structure(c(5L, 4L, 9L, 1L, 8L, 7L, 6L,
2L, 12L, 11L, 10L, 3L), .Label = c("April", "August", "Dezember",
"Februar", "Januar", "Juli", "Juni", "Mai", "März", "November",
"Oktober", "September"), class = "factor"), `06:00:00-00:59:59` = c(1.23650890285152,
1, 0.941959539059369, 0.89618038635695, 0.839845976370379, 0.75789368475892,
0.745357979199247, 0.72726835342115, 0.852384444043199, 0.909567524854388,
0.927385464132519, 0.86987613403725), `06:00:00-08:59:59` = c(0.28594460357425,
0.28900629033325, 0.315719841587, 0.2955345904455, 0.273041491484,
0.26127195229025, 0.23431503049, 0.22167349349575, 0.2603577651915,
0.27084189072775, 0.2653932948955, 0.246001500902), `09:00:00-12:59:59` = c(0.48396300341875,
0.4767075864935, 0.46442852744125, 0.4497871047545, 0.4088477917855,
0.39907331646225, 0.387052062652, 0.3717692228805, 0.39248244105925,
0.422900151297, 0.4111808332435, 0.41687239709375), `13:00:00-16:59:59` = c(0.80304931519625,
0.7588016605465, 0.66548492801425, 0.6354014724685, 0.670969199053,
0.58076568424625, 0.67418744877975, 0.5756792489565, 0.66766765226025,
0.642654220031, 0.665735145053, 0.64571729495575), `17:00:00-19:59:59` = c(1.44101730420325,
1.35385679874675, 1.2576760885645, 1.1478274405785, 1.04871946565175,
0.96498516399125, 0.9775891903885, 0.9959819200865, 1.0805495717725,
1.2042836233065, 1.29337213775425, 1.17905118172475), `20:00:00-22:59:59` = c(2.7590124723295,
2.1458514208575, 2.00826846019375, 1.91719758837025, 1.7425652515095,
1.509920737758, 1.3851778869395, 1.43588733645025, 1.8753361739725,
2.03981388252275, 2.013661241837, 1.79544669375925), `23:00:00-00:59:59` = c(1.92548355216275,
0.926693335582, 0.921407232611, 0.92680356666725, 0.87033626954225,
0.81832375651025, 0.75033232500425, 0.7289554638715, 0.79565322269175,
0.85606802035675, 0.90891474021375, 0.94322847859325)), class = "data.frame", row.names = c(NA,
-12L))
我想建立将一个月设置为“ 1”的索引值。
saisonindex <- RWavg %>%
mutate(`06:00:00-00:59:59` = `06:00:00-00:59:59` / `06:00:00-00:59:59`[Monate == "Februar"])
saisonindex
Monate 06:00:00-00:59:59 06:00:00-08:59:59 09:00:00-12:59:59 13:00:00-16:59:59 17:00:00-19:59:59 20:00:00-22:59:59 23:00:00-00:59:59
1 Januar 1.2365089 0.2859446 0.4839630 0.8030493 1.4410173 2.759012 1.9254836
2 Februar 1.0000000 0.2890063 0.4767076 0.7588017 1.3538568 2.145851 0.9266933
3 März 0.9419595 0.3157198 0.4644285 0.6654849 1.2576761 2.008268 0.9214072
4 April 0.8961804 0.2955346 0.4497871 0.6354015 1.1478274 1.917198 0.9268036
5 Mai 0.8398460 0.2730415 0.4088478 0.6709692 1.0487195 1.742565 0.8703363
6 Juni 0.7578937 0.2612720 0.3990733 0.5807657 0.9649852 1.509921 0.8183238
7 Juli 0.7453580 0.2343150 0.3870521 0.6741874 0.9775892 1.385178 0.7503323
8 August 0.7272684 0.2216735 0.3717692 0.5756792 0.9959819 1.435887 0.7289555
9 September 0.8523844 0.2603578 0.3924824 0.6676677 1.0805496 1.875336 0.7956532
10 Oktober 0.9095675 0.2708419 0.4229002 0.6426542 1.2042836 2.039814 0.8560680
11 November 0.9273855 0.2653933 0.4111808 0.6657351 1.2933721 2.013661 0.9089147
12 Dezember 0.8698761 0.2460015 0.4168724 0.6457173 1.1790512 1.795447 0.9432285
我想动态地对所有数字列执行此操作,我想mutate_if(is.numeric)
会执行此操作,但是我无法理解括号中的条件可能如何工作。
你能帮我吗?
答案 0 :(得分:4)
使用mutate_if
,指定~
,然后将“ Monate”为“ Februar”的列值作为子集
library(dplyr)
RWavg %>%
mutate_if(is.numeric, ~ ./.[Monate == "Februar"])
在base R
中,仅对行进行子集处理,正确确定长度,划分并更新列
RWavg[-1] <- RWavg[-1]/RWavg[RWavg$Monate == 'Februar', -1][col(RWavg[-1])]