我在R中有三个数据表。每个数据表都有一个日期列。表格为dat <- structure(list(Design.ID = c("KCLRI7-00VU-INSGN1", "KPBK4K-00VU-INSGN1",
"KGLGI7-00VU-FETTI1", "KUWB08-00VU-INSGN1", "KUWB08-00VU-INSGN1",
"KGLGI7-00VU-FETTI1", "KPBK4K-00VU-INSGN1", "KCLRI7-00VU-INSGN1",
"KBAMI7-00VU-INSGN1", "KCLRI7-0FLA-WAVE01", "K510WL-0WEB-PRIME1",
"K110MS-0WEB-PRIME1", "KCLRI6-0GON-INSGN1"),
Glados.SKU = c("", "", "", "", "", "", "", "", "", "",
"K510WL-0WEB-PRIME1", "K110MS-0WEB-PRIME1",
"KCLRI6-0GON-INSGN1;KCLRI7-0GON-INSGN1;KCLR7X-0GON-INSGN1;KCLR6X-0GON-INSGN1"
)),
.Names = c("Design.ID", "Glados.SKU"), row.names = c(NA, -13L),
class = "data.frame")
。它们的格式如下:
vix_data,gold_ohlc_data,btc_ohlc_data
如何更改head(vix_data$Date)
[1] 1/2/04 1/5/04 1/6/04 1/7/04 1/8/04 1/9/04
3435 Levels: 1/10/05 1/10/06 1/10/07 1/10/08 1/10/11 ... 9/9/16
head(gold_ohlc_data$date)
[1] 8/23/17 8/22/17 8/21/17 8/18/17 8/17/17 8/16/17
2519 Levels: 1/10/08 1/10/11 1/10/12 1/10/13 1/10/14 ... 9/9/16
head(btc_ohlc_data$Date)
[1] "2017-08-23" "2017-08-22" "2017-08-21" "2017-08-20" "2017-08-19"
[6] "2017-08-18"
和vix_data
表格中的日期列以匹配gold_ohlc_data
格式?我尝试了几种方法,例如使用btc_ohlc_data
来转换每一列 - 但这通常会混淆值并插入很多N / A
答案 0 :(得分:1)
选项是使用包lubridate
中的函数。用户需要知道哪一天是哪一天,哪一个月是选择要使用的正确功能,例如dmy
或mdy
# Load package
library(lubridate)
# Create example string
date1 <- c("1/2/04", "1/5/04", "1/6/04", "1/7/04", "1/8/04", "1/9/04")
date2 <- c("8/23/17", "8/22/17", "8/21/17", "8/18/17", "8/17/17", "8/16/17")
# Convert to date class
dmy(date1)
# [1] "2004-02-01" "2004-05-01" "2004-06-01" "2004-07-01" "2004-08-01" "2004-09-01"
mdy(date1)
# [1] "2004-01-02" "2004-01-05" "2004-01-06" "2004-01-07" "2004-01-08" "2004-01-09"
mdy(date2)
# [1] "2017-08-23" "2017-08-22" "2017-08-21" "2017-08-18" "2017-08-17" "2017-08-16"
答案 1 :(得分:0)
查看包lubridate。 lubridate::dmy()
和ymd()
应处理此问题。
答案 2 :(得分:0)
看起来您的数据是作为因素读入的,因此首先您必须将它们更改为字符。然后,您可以将其转换为日期并指定输入格式,其中%m
代表数字月份,%d
代表日期,%y
代表2位数年份。
x <- c('1/2/04', '1/5/04', '1/6/04', '1/7/04', '1/8/04', '1/9/04')
y <- as.Date(x, format = "%m/%d/%y")
y
[1] "2004-01-02" "2004-01-05" "2004-01-06" "2004-01-07" "2004-01-08"
[6] "2004-01-09"
答案 3 :(得分:0)
您确定要正确指定while (mycursor.moveToNext) {
String mytext = mycursor.getString(mycursor.getColumnIndex("mycolumn"));
mytextView.setText(mytext);
}
mycursor.close();
吗?例如,您有as.Date
而不是%y
吗?
我做了以下工作并且有效:
%Y