mookamooka
mookamooka

Reputation: 463

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.

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.

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.

i.e. should I have change my code to be d <- density(table[dataLine$position==2,]$rt)?

Because that gives me an error saying 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

Upvotes: 0

Views: 188

Answers (1)

Greg Snow
Greg Snow

Reputation: 49640

Your dataline object is a data frame with multiple columns, you need to pass only the column of data to the density function. 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.

Upvotes: 3

Related Questions