2015-11-04 21 views
2

我想繪製data.frame,我的問題是,出現以下錯誤:半徑誤差(...)需要有限「XLIM價值觀

Error in plot.window(...) need finite 'xlim' values 
In addition: Warning messages: 
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion 
2: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion 
3: In min(x) : no non-missing arguments to min; returning Inf 
4: In max(x) : no non-missing arguments to max; returning -Inf 
5: In min(x) : no non-missing arguments to min; returning Inf 
6: In max(x) : no non-missing arguments to max; returning -Inf 

這是我的代碼:

CO2.data = read.table("962RecordedDataCO2.txt",sep=";", stringsAsFactors=F) 

x = CO2.data$V1 
y = CO2.data$V2 

plot(x,y,type="l") 

我想這個問題可能是,x和y是字符值,這不能被繪製(X是日期和時間,例如16.06.2015 20:07:00和y只是一個雙值像0,0300)。但我不能真正做到y = as.numeric(CO2.data$V2)因爲那麼每值NA。

str(CO2.data)結果是:

'data.frame': 24479 obs. of 2 variables: 
$ V1: chr "15.06.2015 00:01:00" "15.06.2015 00:02:00" "15.06.2015 00:03:00" "15.06.2015 00:04:00" ... 
$ V2: chr "0,0200" "0,0200" "0,0200" "0,0200" ... 
+0

能否請你把'STR(CO2.data)'的結果。 – user1436187

+0

當然,我說在我原來的職位。 – Borsi

回答

4

但我不能真正做到y = as.numeric(CO2.data$V2)因爲那麼每值NA。

好吧plot本質上有同樣的問題。

當數據讀出,第一步應始終是把數據成合適的格式,然後才處理下一步驟。您的工作流程應該總是這個樣子的,幾乎沒有例外。

在你的情況,你需要datetime和明確的數值進行轉換,因爲R可不能自動處理的格式轉換:

x = strptime(CO2.data$V1, '%d.%m.%Y %H:%M:%S') 
y = as.numeric(sub(',', '.', CO2.data$V2)) 

特別是,你需要指定的日期格式(第一行)和在將字符串轉換爲數字之前,需要將小數點轉換爲小數點。

如果使用read.csv2,而不是read.table,您可以指定小數點分隔符;這使您可以省略上面的第二個轉變:

CO2.data = read.csv2("962RecordedDataCO2.txt", sep=";", dec = ",", stringsAsFactors=FALSE) 

哦,使用FALSETRUEFT - 後者是變量,所以一些代碼可以重新定義FT意義。

+0

非常感謝! :) – Borsi