Simple R (histogram) from counted csv file [duplicate]

夙愿已清 提交于 2019-12-23 02:52:20

问题


HI Guys I'm trying to plot a frequency graph of a simple 2d file

file:data.csv
terms,count
1,10
5,17
3,28
9,30

I want the first col(terms) to be the x-axis and the col(count) be the height/percentage.

I've tried this:

d<-read.csv(data.csv)
hist(d)
Error in hist.default(d) : 'x' must be numeric
dc<-table(d)
hist(dc)  <-- wrong result.

回答1:


The problem is that hist() needs a vector containing your objects as often as they are present in your data. Your are providing it a frequency table.

See this:

> df <- data.frame(obj = c(1,2,3,4,5), count = c(2,3,5,4,2))

> hist(df)
Error in hist.default(df) : 'x' must be numeric

> hist(rep(df$obj, df$count), breaks=0:5)
[img]

> rep(df$obj, df$count)
 [1] 1 1 2 2 2 3 3 3 3 3 4 4 4 4 5 5

rep(a,n) repeats element by element the value of a n-times. Then you have the vector you need and you can hand it to hist().




回答2:


d<-read.csv(text="terms,count
1,10
5,17
3,28
9,30")

hist(d)   # No error ... but not the plot you wanted.

Your lack of quotes around data.csv could be the problem or if the the first line in the file is really file:data.csv, that could be another problem. It does appear, however, that you probably want barchart or barplot, since you have already done the aggregation of the counts.

To illustrate why barchart or barplot could have been use:

require(lattice)
# dividing by total "counts" to get the fractional values
 barchart(count/sum(d$count)~factor(terms), data=d)



来源:https://stackoverflow.com/questions/20089824/simple-r-histogram-from-counted-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!