我有一個包含每月資料的 data.frame df:
| 公司 | 店鋪 | 品牌 | 月 | 銷售量 | 預算 | 數量 | 年 |
|---|---|---|---|---|---|---|---|
| 一個 | A店 | 品牌A | 君 | 300 美元 | 300 美元 | 3000 | 2022 |
| 一個 | A店 | 品牌A | 七月 | 300 美元 | 300 美元 | 3000 | 2022 |
| 一個 | A店 | 品牌A | 八月 | 300 美元 | 300 美元 | 3000 | 2022 |
| 一個 | A店 | 品牌A | 九月 | 300 美元 | 300 美元 | 3000 | 2022 |
我希望每天都有平均值,例如(Jun 有 30 天,所以銷售額 300 美元 / 30 天 = 每天 10 美元):
| 公司 | 店鋪 | 品牌 | 月 | 銷售量 | 預算 | 數量 | 日期 |
|---|---|---|---|---|---|---|---|
| 一個 | A店 | 品牌A | 君 | 10 美元 | 10 美元 | 100 | 01-06-2022 |
| 一個 | A店 | 品牌A | 君 | 10 美元 | 10 美元 | 100 | 02-06-2022 |
| 一個 | A店 | 品牌A | 君 | 10 美元 | 10 美元 | 100 | 03-06-2022 |
| 一個 | A店 | 品牌A | 君 | 10 美元 | 10 美元 | 100 | 04-06-2022 |
| 一個 | A店 | 品牌A | 君 | 10 美元 | 10 美元 | 100 | 05-06-2022 |
| 一個 | A店 | 品牌A | 君 | 10 美元 | 10 美元 | 100 | 06-06-2022 |
| 一個 | A店 | 品牌A | 君 | 10 美元 | 10 美元 | 100 | 07-06-2022 |
我不知道代碼可以使用什么功能。
謝謝!
uj5u.com熱心網友回復:
前面有幾件事:
我推斷您的語言環境設定為西班牙語(基于
"Ago"我假設是八月)。要運行此代碼,我首先設定我的本地語言環境,以便它能夠正確決議。您可能不需要這個,但其他人(使用其他語言)可能需要這個或類似的東西來測驗這個代碼。prevlocale <- Sys.getlocale("LC_TIME") Sys.setlocale("LC_TIME", "Spanish") # [1] "Spanish_Spain.1252" format(as.Date("2022-08-01"), format = "%b") # [1] "ago" ### when done and you want to return to your local locale Sys.setlocale("LC_TIME", prevlocale)您的數字列不是數字。我會將它們更改為
numeric,否則數學運算將不起作用,您可以根據$需要選擇將它們重新格式化為 -strings。由于每個月有不同的天數,我們不能以簡單的一步邏輯對所有行執行此操作。要向前邁進,第一個挑戰是確定每個月有多少天。有幾種方法可以解決這個問題(包括
lubridate包),我將提供一個 base-R 解決方案(使用as.POSIXlt)來解決它并回傳正確的日期向量。yrmon2days <- function(yr, mon) { stopifnot(length(yr) == 1L, length(mon) == 1L) day2 <- day1 <- as.POSIXlt(as.Date(paste(yr, mon, "01", sep = "-"), format = "%Y-%b-%d")) day2$mon <- day2$mon 1L seq(day1, day2-1, by = "day") } yrmon2days(2022, "Feb") # [1] "2022-02-01 UTC" "2022-02-02 UTC" "2022-02-03 UTC" "2022-02-04 UTC" "2022-02-05 UTC" "2022-02-06 UTC" "2022-02-07 UTC" # [8] "2022-02-08 UTC" "2022-02-09 UTC" "2022-02-10 UTC" "2022-02-11 UTC" "2022-02-12 UTC" "2022-02-13 UTC" "2022-02-14 UTC" # [15] "2022-02-15 UTC" "2022-02-16 UTC" "2022-02-17 UTC" "2022-02-18 UTC" "2022-02-19 UTC" "2022-02-20 UTC" "2022-02-21 UTC" # [22] "2022-02-22 UTC" "2022-02-23 UTC" "2022-02-24 UTC" "2022-02-25 UTC" "2022-02-26 UTC" "2022-02-27 UTC" "2022-02-28 UTC"電流當前不可矢量化;可以這樣做,但是周圍資料還有其他復雜性,這使得目前這一步有點過分了。
我嘗試了
dplyr::group_by總體上使用和分組的道路,但是雖然它是有道理的,但我不想假設每年/每月一行。有了這種預防措施,很明顯我們需要逐行操作,而不是可能(盡管不是使用此資料)回傳每組中的 1 行以外的東西。
dplyr
library(dplyr)
dat %>%
mutate(across(c(Sales, Budget), ~ as.numeric(gsub("\\$", "", .)))) %>%
rowwise() %>%
summarize(
Date = yrmon2days(Year, Month),
Company, Store, Brand, Year, Month,
across(c(where(is.numeric), -Year), ~ . / length(Date))
)
# # A tibble: 122 x 9
# Date Company Store Brand Year Month Sales Budget Quantity
# <dttm> <chr> <chr> <chr> <int> <chr> <dbl> <dbl> <dbl>
# 1 2022-06-01 00:00:00 A Store A Brand A 2022 Jun 10 10 100
# 2 2022-06-02 00:00:00 A Store A Brand A 2022 Jun 10 10 100
# 3 2022-06-03 00:00:00 A Store A Brand A 2022 Jun 10 10 100
# 4 2022-06-04 00:00:00 A Store A Brand A 2022 Jun 10 10 100
# 5 2022-06-05 00:00:00 A Store A Brand A 2022 Jun 10 10 100
# 6 2022-06-06 00:00:00 A Store A Brand A 2022 Jun 10 10 100
# 7 2022-06-07 00:00:00 A Store A Brand A 2022 Jun 10 10 100
# 8 2022-06-08 00:00:00 A Store A Brand A 2022 Jun 10 10 100
# 9 2022-06-09 00:00:00 A Store A Brand A 2022 Jun 10 10 100
# 10 2022-06-10 00:00:00 A Store A Brand A 2022 Jun 10 10 100
# # ... with 112 more rows
堿基R
dat[c("Sales","Budget")] <- lapply(dat[c("Sales","Budget")], function(z) as.numeric(gsub("\\$", "", z,)))
isnum <- sapply(dat, is.numeric)
isnum[which(colnames(dat) == "Year")] <- FALSE
out <- do.call(rbind, lapply(seq_len(nrow(dat)), function(rn) {
Date <- yrmon2days(dat$Year[rn], dat$Month[rn])
Nums <- lapply(dat[rn,isnum], `/`, length(Date))
suppressWarnings( # "row names were found from a short variable and have been discarded"
cbind(dat[rn,!isnum], Nums, data.frame(Date = Date))
)
}))
head(out)
# Company Store Brand Month Year Sales Budget Quantity Date
# 1 A Store A Brand A Jun 2022 10 10 100 2022-06-01
# 2 A Store A Brand A Jun 2022 10 10 100 2022-06-02
# 3 A Store A Brand A Jun 2022 10 10 100 2022-06-03
# 4 A Store A Brand A Jun 2022 10 10 100 2022-06-04
# 5 A Store A Brand A Jun 2022 10 10 100 2022-06-05
# 6 A Store A Brand A Jun 2022 10 10 100 2022-06-06
資料
dat <- structure(list(Company = c("A", "A", "A", "A"), Store = c("Store A", "Store A", "Store A", "Store A"), Brand = c("Brand A", "Brand A", "Brand A", "Brand A"), Month = c("Jun", "Jul", "Ago", "Sep"), Sales = c("$300", "$300", "$300", "$300"), Budget = c("$300", "$300", "$300", "$300"), Quantity = c(3000L, 3000L, 3000L, 3000L), Year = c(2022L, 2022L, 2022L, 2022L)), class = "data.frame", row.names = c(NA, -4L))
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/410996.html
標籤:
上一篇:如何使用luxon(DateTimeNow)和ReactJs正確映射/過濾資料
下一篇:VaadinGrid可排序日期列
