情节分类变量

问题描述 投票:2回答:1

我是R语言的新手,并按照说明幻灯片进行绘制:

enter image description here

survey[["Program"]]是数据帧中的分类数据列。

> survey[["Program"]]  # returns the Program column as a vector
 [1] "PPM"   "PPM"   "PPM"   "Other" "PPM"   "PPM"   "PPM"   "Other" "PPM"   "Other" "MISM"  "PPM"   "MISM" 
[14] "Other" "PPM"   "PPM"   "PPM"   "PPM"   "PPM"   "Other" "PPM"   "MISM"  "PPM"   "PPM"   "PPM"   "MISM" 
[27] "PPM"   "Other" "Other" "PPM"   "Other"

但是,当我实现plot(survey[["Program"]])时,出现错误:

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

我不知道为什么不能得到与图中所示相同的结果。

r plot
1个回答
3
投票

该绘图命令仅适用于factor列。 (嗯,它在很多事情上都起作用,但是这里的困惑是因为变量不是一个因素。)比较例如]

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

with

plot(factor(c('a', 'a', 'b', 'b', 'c')))

enter image description here

您可以通过调用str(survey)来检查列的类。您可能以与假定的幻灯片不同的方式读入数据。

您可以使用

plot(factor(survey[["Program"]]))

或者也许

barplot(table(survey[["Program"]]))

获得相同的结果。

© www.soinside.com 2019 - 2024. All rights reserved.