我知道從 Excel 到 R 的過渡需要一些學習。在這里,我想根據 if else 函式更新列“強度”。在 Excel 中,這將被讀取為 Time =100 然后強度將與 Rainfall 中的值相同對于其他任何事情它將是下一個單元格中的值減去上面的單元格值(例如 =IF(C2=1000,D2,D2 -D1).我以我有限的知識嘗試了一些簡單的代碼,但無法通過。此外,我想將Time列下的值更改為適當的時間值。可能這應該在if else之前處理. 以下是我迄今為止設法取得進展的代碼。感謝所有輸入。
setwd ("C:/Users/")
files <- list.files(path="C:/Users/", full.names = T, recursive = F)
listcsv <- lapply(files, function(x) read.csv(paste0(x)))
data <- do.call(rbind, lapply
(files, read.csv, as.is=T, skip = 6, header = TRUE))
keeps <- data[c("Date", "Time", "Rainfall")]
keeps$Date_Formatted <- as.Date(keeps$Date, format = "%d/%m/%Y")
dateorder <- keeps[order(keeps$Date_Formatted,decreasing = FALSE) , ]
dateorder['Intensity'] <- NA
dput(dateorder[1:10, ])
uj5u.com熱心網友回復:
這最好在分組方法中完成。
dplyr
library(dplyr)
dateorder %>%
group_by(grp = cumsum(Time == 1000)) %>%
mutate(Intensity = c(Rainfall[1], diff(Rainfall))) %>%
ungroup() %>%
select(-grp)
# # A tibble: 10 x 5
# Date Time Rainfall Date_Formatted Intensity
# <chr> <int> <dbl> <date> <dbl>
# 1 30/04/2021 1000 0.4 2021-04-30 0.4
# 2 30/04/2021 1100 0.4 2021-04-30 0
# 3 30/04/2021 1200 0.6 2021-04-30 0.200
# 4 30/04/2021 1300 0.8 2021-04-30 0.2
# 5 30/04/2021 1400 0.8 2021-04-30 0
# 6 30/04/2021 1500 1 2021-04-30 0.200
# 7 30/04/2021 1600 0 2021-04-30 -1
# 8 30/04/2021 1700 0 2021-04-30 0
# 9 30/04/2021 1800 0 2021-04-30 0
# 10 30/04/2021 1900 0 2021-04-30 0
基數R
grp <- cumsum(dateorder$Time == 1000)
dateorder$Intensity <- ave(dateorder$Rainfall, grp, FUN = function(z) c(z[1], diff(z)))
dateorder
# Date Time Rainfall Date_Formatted Intensity
# 1 30/04/2021 1000 0.4 2021-04-30 0.4
# 2 30/04/2021 1100 0.4 2021-04-30 0.0
# 3 30/04/2021 1200 0.6 2021-04-30 0.2
# 4 30/04/2021 1300 0.8 2021-04-30 0.2
# 5 30/04/2021 1400 0.8 2021-04-30 0.0
# 6 30/04/2021 1500 1.0 2021-04-30 0.2
# 7 30/04/2021 1600 0.0 2021-04-30 -1.0
# 8 30/04/2021 1700 0.0 2021-04-30 0.0
# 9 30/04/2021 1800 0.0 2021-04-30 0.0
# 10 30/04/2021 1900 0.0 2021-04-30 0.0
資料
dateorder <- structure(list(Date = c("30/04/2021", "30/04/2021", "30/04/2021", "30/04/2021", "30/04/2021", "30/04/2021", "30/04/2021", "30/04/2021", "30/04/2021", "30/04/2021"), Time = c(1000L, 1100L, 1200L, 1300L, 1400L, 1500L, 1600L, 1700L, 1800L, 1900L), Rainfall = c(0.4, 0.4, 0.6, 0.8, 0.8, 1, 0, 0, 0, 0), Date_Formatted = structure(c(18747, 18747, 18747, 18747, 18747, 18747, 18747, 18747, 18747, 18747 ), class = "Date"), Intensity = c(0.4, 0, 0.2, 0.2, 0, 0.2, -1, 0, 0, 0)), row.names = c(NA, 10L), class = "data.frame")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/384791.html
