我想绘制一个data.frame,我的问题是,出现以下错误:
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
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
CO2.data = read.table("962RecordedDataCO2.txt",sep=";", stringsAsFactors=F)
x = CO2.data$V1
y = CO2.data$V2
plot(x,y,type="l")
Run Code Online (Sandbox Code Playgroud)
我认为问题可能是,x和y是字符值,这不能被绘制(例如16.06.2015 20:07:00,x是日期和时间,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" ...
Run Code Online (Sandbox Code Playgroud)
但我真的不能这样做,
y = as.numeric(CO2.data$V2)因为那时每个值都是NA.
好吧plot基本上有同样的问题.
读取数据时,第一步应始终是将数据放入适当的格式,然后才能处理下一步.您的工作流程应始终如此,几乎没有例外.
在您的情况下,您需要显式转换日期时间和数值,因为R无法自动处理格式转换:
x = strptime(CO2.data$V1, '%d.%m.%Y %H:%M:%S')
y = as.numeric(sub(',', '.', CO2.data$V2))
Run Code Online (Sandbox Code Playgroud)
特别是,您需要指定日期格式(第一行),并且需要在将字符串转换为数字之前将十进制逗号转换为小数点.
如果使用read.csv2而不是read.table,则可以指定小数分隔符; 这允许您省略上面的第二次转换:
CO2.data = read.csv2("962RecordedDataCO2.txt", sep=";", dec = ",", stringsAsFactors=FALSE)
Run Code Online (Sandbox Code Playgroud)
哦,使用FALSE和TRUE,不 F和T-后者是变量,所以一些代码可以重新定义的含义F和T.
| 归档时间: |
|
| 查看次数: |
29551 次 |
| 最近记录: |