简体   繁体   English

从R中的表格绘制密度

[英]Plotting densities from a table in R

I'm currently trying to plot densities from a table/list where column 1 is just the names/labels of the data and column 2 is the actual numbers I want plotted. 我目前正在尝试从表/列表中绘制密度,其中第1列只是数据的名称/标签,第2列是我要绘制的实际数字。

If I just have a file of numbers, I can get my code to work using the scan function. 如果我只有一个数字文件,则可以使用扫描功能来使代码正常工作。 However when I have my table, I get a argument 'x' must be numeric error. 但是,当我有表时,得到的argument 'x' must be numeric错误。

Here is my code: 这是我的代码:

library(gplots)
dataLine = read.table("fileLocation")

den = density(dataLine)

plot(den, col = "red", ylim = c(0, 50), xlim = c(0.045,1))

Should I be using something like what this previous question has done with selecting for a specific column? 我是否应该使用类似上一个问题所做的选择特定列的操作? Plotting densities in R . R中的绘制密度

ie should I have change my code to be d <- density(table[dataLine$position==2,]$rt) ? 即我应该将代码更改为d <- density(table[dataLine$position==2,]$rt)吗?

Because that gives me an error saying object of type'closure' is not subsettable 因为那给了我一个错误,说object of type'closure' is not subsettable

Any suggestions on what I should do to plot my column of numbers? 关于应如何绘制数字列的任何建议? Apologies if my code doesn't make sense; 抱歉,如果我的代码没有意义; I'm fairly new to R 我对R很陌生

Your dataline object is a data frame with multiple columns, you need to pass only the column of data to the density function. 您的数据线对象是一个具有多列的数据框,您只需要将数据列传递给density函数。 To pass the 2nd column of the data frame you can do this: 要传递数据框的第二列,您可以执行以下操作:

density(dataline[[2]])

See the help page ?'[[' for more options to use just part of a data frame. 请参阅帮助页面?'[['了解更多选项,以仅使用数据框的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM