R编程中将字符串转换为日期格式——as.Date()函数
R 语言中的as.Date()
函数用于将字符串转换为日期格式。
Syntax: as.Date(x, format)
Parameters:
x: string variable
format: Format in which string is declared(%m/%d/%y)
示例 1:
# R program to convert string into date
# Creating a string vector
dates <- c("27 / 02 / 92")
# Conversion into date format
result<-as.Date(dates, "% d/% m/% y")
# Print result
print(result)
输出:
[1] "1992-02-27"
示例 2:
# R program to convert string into date
# Creating a string vector
dates <- c("02 / 27 / 92", "02 / 27 / 92",
"01 / 14 / 92", "02 / 28 / 92",
"02 / 01 / 92")
# Conversion into date format
result<-as.Date(dates, "% m/% d/% y")
# Print result
print(result)
输出:
[1] "1992-02-27" "1992-02-27" "1992-01-14" "1992-02-28" "1992-02-01"