2017-04-11 21 views
0

我對r很新穎,所以想知道是否有人可能以幫助我收到的錯誤消息。將POSIXct中的data.frame列轉換爲numeric將生成:「as.POSIXct.numeric(value)中的錯誤:必須提供'origin'

我有一個data.frame AUC_sheet其中包含AUC_sheet$sys_time列在POSIXct並表示在操作過程中採取血壓讀數的時間。

我想將AUC_sheet轉換成POSIXct,這樣我可以在曲線計算下的後續區域獲得準確的結果。我用以下for循環,執行轉換:

for(i in 1:length(AUC_sheet$sys_time)){ 
AUC_sheet$sys_time[i] <- as.numeric(difftime(time1 = AUC_sheet$sys_time[1], 
              time2 = AUC_sheet$sys_time[i], units = "hours")) 
} 

,但我一直如下

Error in as.POSIXct.numeric(value) : 'origin' must be supplied 

我一直在使用origin = "1970-01-01"試圖得到一個錯誤信息,但它告訴我,這是一個未使用的論據。

有什麼顯而易見的,我做錯了嗎?

在此先感謝,和抱歉,如果我不是提供了足夠的數據,如果需要的話

我可以發佈更多的作爲編輯

編輯 AUC_sheet $ SYS_TIME看起來像這樣

 sys_value   sys_time 
     <dbl>    <time> 
1   85 2013-08-28 12:48:24 
2   NA 2013-08-28 12:48:39 
3   NA 2013-08-28 12:48:54 
4   NA 2013-08-28 12:49:08 
5   NA 2013-08-28 12:49:24 
6  170 2013-08-28 12:49:38 
7  150 2013-08-28 12:49:54 
8  167 2013-08-28 12:50:09 
9  175 2013-08-28 12:50:24 
10  167 2013-08-28 12:50:39 
# ... with 549 more rows 
+1

介意顯示一些sys_time的例子嗎? – din

+0

@CPhil,感謝您的幫助和解釋。完美工作 – cmo1

回答

1

你的問題是不是as.numeric自己調用。問題是你正試圖把這個調用的結果寫到一個POSIXct列的列上。因此,R會嘗試將其轉換爲適合您的正確格式,並且會失敗,因爲轉換方法需要一個原點。 如果你寫了一個新的列(或者,更好的是,寫for循環作爲一個矢量化操作來避免這個問題),那麼你應該沒有問題。

# make up some dummy data for testing 
AUC = data.frame(sys_value = 1:100, sys_time = as.POSIXct(Sys.time() + 1:100)) 

for(i in 1:length(AUC$sys_time)){ 
    AUC$sys_time[i] <- as.numeric(difftime(time1 = AUC$sys_time[1], 
            time2 = AUC$sys_time[i], units = "hours")) 
} # this doesn't work, because you're mixing data types in the sys_time column 


for(i in 1:length(AUC$sys_time)){ 
    AUC$sys_time_diff[i] <- as.numeric(difftime(time1 = AUC$sys_time[1], 
              time2 = AUC$sys_time[i], units = "hours")) 
} # this works, because the numeric data isn't being added to a time column 
相關問題