【发布时间】:2019-01-03 21:14:29
【问题描述】:
下面是我目前用来总结我的数据的代码,它正在工作。我的问题是我想实际取“CE100”列的平均值与总和。我怎样才能操纵下面的代码来做到这一点?
library(data.table, warn.conflicts = FALSE)
library(magrittr) ### MODIFIED
# library(lubridate, warn.conflicts = FALSE) ### MODIFIED
################
## PARAMETERS ##
################
# Set path of major source folder for raw transaction data
in_directory <- "C:/Users/NAME/Documents/Raw Data/"
# List names of sub-folders (currently grouped by first two characters of CUST_ID)
in_subfolders <- list("AA-CA", "CB-HZ", "IA-IL", "IM-KZ", "LA-MI", "MJ-MS",
"MT-NV", "NW-OH", "OI-PZ", "QA-TN", "TO-UZ",
"VA-WA", "WB-ZZ")
# Set location for output
out_directory <- "C:/Users/NAME/Documents/YTD Master/"
out_filename <- "OUTPUT.csv"
# Set beginning and end of date range to be collected - year-month-day format
date_range <- c("2018-01-01", "2018-06-30") ### MODIFIED
# Enable or disable filtering of raw files to only grab items bought within certain months to save space.
# If false, all files will be scanned for unique items, which will take longer and be a larger file.
# date_filter <- TRUE ### MODIFIED
##########
## CODE ##
##########
starttime <- Sys.time()
# create vector of filenames to be processed
in_filenames <- list.files(
file.path(in_directory, in_subfolders),
pattern = "\\.txt$",
full.names = TRUE,
recursive = TRUE)
# filter filenames, only
selected_in_filenames <-
seq(as.Date(date_range[1]),
as.Date(date_range[2]), by = "1 month") %>%
format("%Y-%m") %>%
lapply(function(x) stringr::str_subset(in_filenames, x)) %>%
unlist()
# read and aggregate each file separetely
mastertable <- rbindlist(
lapply(selected_in_filenames, function(fn) {
message("Processing file: ", fn)
temptable <- fread(fn,
colClasses = c(CUSTOMER_TIER = "character"),
na.strings = "")
{ # Add columns
print(paste0("Adding columns - ", subfolder, " (", j," of ", length(in_subfolders), ")"))
print(Sys.time()-starttime)
temptable[, ':='(CustPart = paste0(CUST_ID, INV_ITEM_ID))]}
# aggregate file but filtered for date_range
temptable[INVOICE_DT %between% date_range,
lapply(.SD, sum), by = .(CustPart, QTR = quarter(INVOICE_DT), YEAR = year(INVOICE_DT)),
.SDcols = c("Ext Sale", "CE100")]
})
)[
# second aggregation overall
, lapply(.SD, sum), by = .(CustPart, QTR, YEAR), .SDcols = c("Ext Sale", "CE100")]
# Save Final table
print("Saving master table")
fwrite(mastertable, file.path(out_directory, out_filename))
# rm(mastertable) ### MODIFIED
print(Sys.time()-starttime)
mastertable
我已经包含了我的所有代码,以显示我如何读取我的数据。如果需要任何其他细节,比如一些示例数据,请告诉我。
【问题讨论】:
-
你有
lapply(.SD, sum)。您是否只想将sum更改为mean?我不明白。在寻求帮助时,您应该添加一个minimal reproducible example。删除与您的问题没有直接关系的任何内容并包含示例数据,以便您的代码可以实际运行。
标签: r sum data.table mean