First Last
First Last

Reputation: 649

Plotting uneven row sizes in R

I have data in tab delimited rows of uneven length and I want to make a histogram for each row:

1    23    352    4    12    94    0    2
434    13    29
5    93    93    34
(...more rows)

This is what I currently have (no fanciness included):

data = read.delim(file.txt,header = F, sep="\t")
for (j in 1:nrow(data)) { #loop over each row
    hist(data[j,])

But when I try to make the histogram, I think it tries to include the NA's in the row of the data frame, since R gives me the error message: "Error in hist.default(data[2, ]) : 'x' must be numeric".

When I try to use:

read.scan("file.txt, sep="\t")

I'm left with something I don't know how to separate by rows. Do I have a better option than splitting the file into one row per file and then reading in each row separately? (I am running into the same problem with uneven column size...)

Upvotes: 0

Views: 192

Answers (1)

bdemarest
bdemarest

Reputation: 14667

The error results from the fact that grabbing a row from a data.frame yields an object of class data.frame (and hist() wants class numeric). Just convert it to numeric:

hist(as.numeric(data[j,]))

Upvotes: 2

Related Questions