将日期格式从dd-mm-yyyy转换为dd / mm / yyyy

时间:2020-09-10 02:03:43

标签: r date-format

我想将日期格式从dd-mm-yyyy转换为dd / mm / yyyy

数据:

#_______________ Sigmoid function to smooth steps _______________
xs = np.array(x)
ys = np.array(y)

diff = ys[1:] - ys[:-1]
indexBool = diff > 0.385 # Variable adjusted to fit number of steps
index = np.argwhere(indexBool).reshape(-1)

step1x = xs[(index[0]-100):(index[0]+100)]
step1y = ys[(index[0]-100):(index[0]+100)]
step2x = xs[(index[1]-100):(index[1]+100)]
step2y = ys[(index[1]-100):(index[1]+100)]
step3x = xs[(index[2]-100):(index[2]+100)]
step3y = ys[(index[2]-100):(index[2]+100)]

def sigmoid(x):
    return (1 / (1 + np.exp(-x)))

plt.figure()
plt.plot(xs, ys)
# plt.plot(step1x, step1y)
# plt.plot(step2x, step2y)
# plt.plot(step3x, step3y)
plt.plot(step1x, sigmoid(step1y))
plt.plot(step2x, sigmoid(step2y))
plt.plot(step3x, sigmoid(step3y))

plt.show()

当前代码:

         date
1 22-Jul-2020

所需的输出:

format(as.Date(df$date, '%d:%m:%Y'), '%d/%m/%Y' )
[1] NA NA

2 个答案:

答案 0 :(得分:3)

format中的as.Date应该与输入格式匹配。它是%d,后跟-,然后是月份的缩写(%b),然后是-和4位数字的年份(%Y

df$date <- format(as.Date(df$date, '%d-%b-%Y'), '%d/%m/%Y' )
df$date
#[1] "22/07/2020"

数据

df <- structure(list(date = "22-Jul-2020"), class = "data.frame", row.names = "1")

答案 1 :(得分:0)

您可以尝试

library(lubridate)
df <- data.frame(date = c("22-Jul-2020"))

df$date <- dmy(df$date)
df$date <- format(df$date, format = "%d/%m/%Y")
#       date
#1 22/07/2020