我有一个日期为格式的字符列10/17/2017 12:00:00 AM.我想只保留日期部分即2017-10-17.我在用 -
df$ReportDate = as.Date(df$ReportDate, format = "%m/%d/%Y %I:%M:%S %p")
df$ReportDate = as.Date(format(df$ReportDate, "%Y-%m-%d"))
Run Code Online (Sandbox Code Playgroud)
这是有效的,但数据框有超过500万行,所以这需要接近两分钟.
user system elapsed
104.73 0.55 105.46
Run Code Online (Sandbox Code Playgroud)
有没有更快,更有效的方法来做到这一点?
我正在尝试将 Excel 文件转换为 PDF。在 python和Python 中使用Print selected worksheets in excel files to pdf - Converting XLSX to PDF,我编写了下面的代码。
这可以毫无问题地将 Excel 转换为 PDF,但会打开 Excel 文件。我认为这样做的目的.Visible = False是为了防止这种情况发生?我希望 excel 对象保持隐藏状态,因为我正在对 100 多个文件执行此操作,并且我不希望 excel 打开 100 次。
import win32com.client
import os
import re
nm = 'Sample.xlsx'
excel = win32com.client.Dispatch("Excel.Application")
excel.Visible = False
wb = excel.Workbooks.Open('{0}\\{1}'.format(os.getcwd(), nm))
wb.WorkSheets('Report').Select()
nm_pdf = re.sub('.xlsx', '.pdf', nm, count = 1)
wb.ActiveSheet.ExportAsFixedFormat(0, '{0}\\{1}'.format(os.getcwd(), nm_pdf))
#excel.Quit()
Run Code Online (Sandbox Code Playgroud) 我有一个带日期的数据框,我正在尝试将其转换为 POSIXct 对象,但我无法指定时区。知道为什么会这样吗?
> str(dates)
'data.frame': 3171 obs. of 3 variables:
$ Date : Date, format: "2013-05-14" "2013-08-15" "2014-05-30" "2014-09-29" ...
$ BB_Ticker: Factor w/ 1252 levels "A US Equity",..: 1 2 2 2 2 2 2 2 2 2 ...
$ 1Y : POSIXct, format: "2013-05-13 20:00:00" "2013-08-14 20:00:00" "2014-05-29 20:00:00" "2014-09-28 20:00:00" ..
Run Code Online (Sandbox Code Playgroud)
我尝试指定"America/New_York"以及"EST5EDT"但它没有效果 -
> head(as.POSIXct(dates$Date, tz = "GMT"), 3)
[1] "2013-05-13 20:00:00 EDT" "2013-08-14 20:00:00 EDT" "2014-05-29 20:00:00 EDT"
> head(as.POSIXct(dates$Date, tz …Run Code Online (Sandbox Code Playgroud) 我有一个名称列表,我需要从"名字姓氏"转换为"姓氏,名字".
Barack Obama
Donald J. Trump
J. Edgar Hoover
Beyonce Knowles-Carter
Sting
Run Code Online (Sandbox Code Playgroud)
我使用了G. Grothendieck对"姓氏,名字" - >"名字姓氏"的回答,序列化的字符串给gsub("([^ ]*) ([^ ]*)", "\\2, \\1", str)了我 -
Obama, Barack
J., DonaldTrump,
Edgar, J.Hoover,
Knowles-Carter, Beyonce
Sting
Run Code Online (Sandbox Code Playgroud)
我想得到什么 -
Obama, Barack
Trump, Donald J.
Hoover, J. Edgar
Knowles-Carter, Beyonce
Sting
Run Code Online (Sandbox Code Playgroud)
我想要一个正则表达式的答案.
我遇到emf()了devEMF包中的功能问题。
我正在使用的代码 -
library(devEMF)
emf(file = "trial.emf")
plot(1:10, seq(10, 100, 10), type = "l", xlab = "Time", ylab = "Distance")
#sample plot
dev.off()
Run Code Online (Sandbox Code Playgroud)
这没有绘图线,但有其他所有内容(标签、轴刻度、标题)。
我错过了一些图形包吗?
sessionInfo()
#R version 3.3.2 (2016-10-31)
#Platform: x86_64-w64-mingw32/x64 (64-bit)
#Running under: Windows 7 x64 (build 7601) Service Pack 1
#locale:
#[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United #
#States.1252
#[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
#[5] LC_TIME=English_United States.1252
#attached base packages:
#[1] stats graphics grDevices utils datasets methods base
#other attached packages:
#[1] devEMF_3.6
#loaded via a …Run Code Online (Sandbox Code Playgroud)