plot.window(...)中的R错误需要有限的'xlim'值 [英] R error in plot.window(...) need finite 'xlim' values

查看:5711
本文介绍了plot.window(...)中的R错误需要有限的'xlim'值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个data.frame,我的问题是出现以下错误:

I want to plot a data.frame and my problem is, that the following error appears:

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.

I thought the problem might be, that x and y are character values and this cannot be plotted (x are dates and times for example 16.06.2015 20:07:00 and y just a double value like 0,0300). But I couldn't really do y = as.numeric(CO2.data$V2) because then every value was 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" ...

推荐答案

但是我真的不能做y = as.numeric(CO2.data$V2),因为那时每个值都是NA.

But I couldn't really do y = as.numeric(CO2.data$V2) because then every value was NA.

好吧plot本质上也有同样的问题.

Well plot essentially has the same problem.

在读取数据时,第一步始终应该是将数据放入适当的格式,然后再进行下一步.您的工作流程应该总是这样,几乎没有例外.

When reading in data, the first step should always be to put the data into an appropriate format, and only then process to the next step. Your workflow should always look like this, with virtually no exceptions.

在这种情况下,您需要显式转换日期时间和数字值,因为R无法自动处理格式转换:

In your case, you need to convert the datetime and the numeric values explicitly because R cannot handle the format conversion automatically:

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

尤其是,您需要指定日期格式(第一行),并且需要在将字符串转换为数字之前将十进制逗号转换为小数点.

In particular, you need to specify the date format (first line) and you need to convert decimal commas into decimal points before converting the strings to numbers.

如果使用read.csv2而不是read.table,则可以指定小数点分隔符;这样您就可以省略上面的第二次转换:

If you use read.csv2 instead of read.table, you can specify the decimal separator; this allows you to omit the second conversion above:

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

哦,并使用FALSETRUE不是 FT —后者是变量,因此某些代码可以重新定义F.

Oh, and use FALSE and TRUE, not F and T — the latter are variables, so some code could redefine the meaning of F and T.

这篇关于plot.window(...)中的R错误需要有限的'xlim'值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆