简单的R(直方图)来自计数的csv文件 [英] Simple R (histogram) from counted csv file

查看:216
本文介绍了简单的R(直方图)来自计数的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  file:data.csv $ b我试图绘制一个简单的2d文件的频率图$ b表款
1,10
5,17
3,28
9,30

我希望第一个col(词)是x轴,col(count)是高度/百分比。



  d <-read.csv(data.csv)
hist( d)
hist.default(d)中的错误:'x'必须是数字
dc< -table(d)
hist(dc)< - 错误的结果。


解决方案

问题在于 hist ()需要一个包含对象的向量,就像它们出现在数据中一样。您可以提供一个频率表。



请看:

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

> hist(df)
hist.default(df)中的错误:'x'必须是数字

> 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 4 4 4 4 5 5



rep(a,n)按元素重复 a <$ c的值$ C>名词 -times。然后你有你需要的矢量,你可以把它交给 hist()


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.

解决方案

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().

这篇关于简单的R(直方图)来自计数的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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