2013-10-29 55 views
0

一個不尋常的請求的位,但我試圖在數據幀的每個第二列中舍入日期時間。我想我已經可以識別每隔一列(使用df[c(T,F)],但我有麻煩工作如何改造適用於這些列在數據幀的每個第二列舍入日期

以下是我目前正在使用:

for (ci in 1:ncol(df1[c(T,F)])) { 
    ci<-round.POSIXt(as.Date(df[c(T,F)]),format = "%d/%m/%Y %H:%M") 
} 

我「M還具有麻煩轉換當前日戳到日期,因爲它們被存儲作爲在以下格式2013年10月24日0點19分00秒的因素我已經嘗試了一些事情,包括:

as.POSIXct(strptime(as.numeric((df[2], "%Y/%m/%d %H:%M:%S")) 

strptime(df[2], format='%Y/%m/%d %H:%M:%S') 

但我不斷收到以下錯誤:

Error in as.Date.default(a[1]) : 
    do not know how to convert 'a[1]' to class "Date" 

編輯:重複的例子

我用dput在我的數據幀再現第一3行和6列,aplogies輸出的長度(I假設這是由於日期被存儲作爲目前的因素)。

structure(list(Sample.Time..Trend.1. = structure(2:4, .Label = c("", 
                  "2013/10/24 00:19:00", "2013/10/24 00:49:00", "2013/10/24 01:18:59", 
                  "2013/10/24 01:48:59", "2013/10/24 02:18:59", "2013/10/24 02:48:59", 
                  "2013/10/24 03:18:59", "2013/10/24 03:48:59", class = "factor"), AHU.DJ_SATemp = c(23.5765, 
                                  23.5814, 23.5814), Sample.Time..Trend.2. = structure(2:4, .Label = c("", 
                                                   "2013/10/24 00:19:00", "2013/10/24 00:49:00", "2013/10/24 01:18:59", 
                                                   "2013/10/24 01:48:59", "2013/10/24 02:18:59", "2013/10/24 02:48:59", 
                                                   "2013/10/24 03:18:59", "2013/10/24 03:48:59"), class = "factor"), AHU.DJ_RATemp = c(23.5814, 
                                                                  23.5814, 23.4886), Sample.Time..Trend.3. = structure(1:3, .Label = c("2013/10/21 22:30:00", 
                                                                                   "2013/10/21 23:00:00", "2013/10/21 23:30:00", "2013/10/22 00:00:00", 
                                                                                   "2013/10/22 00:30:00", "2013/10/22 01:00:00", "2013/10/22 01:30:00", 
                                                                                   "2013/10/22 02:00:00", "2013/10/22 02:30:00", "2013/10/22 03:00:00", 
                                                                                   "2013/10/22 03:30:00"), class = "factor"), AHU.DJ_HWValve = c(0, 
                                                                                                   0, 0)), .Names = c("Sample.Time..Trend.1.", "AHU.DJ_SATemp", 
                                                                                                        "Sample.Time..Trend.2.", "AHU.DJ_RATemp", "Sample.Time..Trend.3.", 
                                                                                                        "AHU.DJ_HWValve"), row.names = c(NA, 3L), class = "data.frame") 

EDIT2:工作代碼:

終於得到了這個,非常感謝工作下面@Henrik。這裏是代碼的最終版本:

library(lubridate) 
try(df<- read.csv("Trends.csv")) 

# convert factor versions of dates to as.POSIXct 
df[c(TRUE, FALSE)] <- lapply(df[c(TRUE, FALSE)], function(x){ 
    as.POSIXlt(strptime(x, , format='%Y/%m/%d %H:%M:%S')) 
}) 
str(df) 

# round every second columns to nearest half-hour 
df[c(TRUE, FALSE)] <- lapply(df[c(TRUE, FALSE)], function(x){ 
    format(as.POSIXlt(round(as.double(x)/(30*60))*(30*60),origin=(as.POSIXlt('1970-01-01'))),format='%d/%m/%Y %H:%M') 
    } 
) 

# Loop through data frame and output results to file 
for (ci in 1:ncol(df)) { 
    a<-na.omit(cbind(df[ci-1],df[ci])) 
    write.csv(a, paste(colnames(df[ci]), ".csv",sep = ""),quote=FALSE,row.names=FALSE) 
} 
+3

你更如果您提供[最小,可重現的示例],可能會收到一個有用的答案(http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610)。乾杯。 – Henrik

+1

關於因素問題,你需要做'as.Date(as.character(,「%Y /%m /%d%H:%M:%S」))''。至於你的其他問題,就像@亨利克建議的那樣,一個例子會有幫助。 – TheComeOnMan

+0

好的,我的道歉,我會編輯我的問題,以添加一個例子 –

回答

1

出於某種原因,您的示例數據止跌回升有點怪我,所以我做了一個小的數據集:

library(lubridate) 

# some test data with dates as factors 
tt <- as.factor(c(Sys.time(), Sys.time())) 
df <- data.frame(a = tt, b = tt, c = tt, d = tt) 
str(df) 

# convert factor versions of dates to as.POSIXct 
df[] <- lapply(df, function(x) ymd_hms(as.character(x))) 
str(df) 

# round every second columns to nearest minut 
df[c(TRUE, FALSE)] <- lapply(df[c(TRUE, FALSE)], function(x){ 
    round_date(x, "minute") 
} 
          ) 
str(df) 
df 
#      a     b     c     d 
# 1 2013-10-29 17:26:00 2013-10-29 17:26:20 2013-10-29 17:26:00 2013-10-29 17:26:20 
# 2 2013-10-29 17:26:00 2013-10-29 17:26:20 2013-10-29 17:26:00 2013-10-29 17:26:20 
相關問題