[R] How to add elements to a table
bioinfonews at pt.lu
bioinfonews at pt.lu
Fri Jun 29 10:43:12 CEST 2007
Hi,
I've been using R for a few weeks now, but consider myself still a
newbie, especially in what concerns the basics of data handling in R.
My situation is this:
I read in data from a database using RODBC, and then use "table" to
produce a table of "counts per years", table which I give to "plot".
All is well, as long as there are no gaps in the years sequence, but
now I have the following result (example):
1990 20
1991 15
1995 17
1997 9
1998 10
1999 11
2000 5
The "plot" function is quite intelligent, in the sense that it draws
appropriate gaps in the x-axis between years, but not intelligent
enough to interrupt the data line during the gap. This gives the
impression that, although no year is marked on the x-axis between 1991
and 1995, there has been data for this period, which is not correct.
What I tried to do is convert the table to a matrix, insert zeros for
the missing years using rbind and cbind, and convert the result back
to table. But the structure of this resulting table is not the same as
for the originating table, so that I need to pass "tab[1,]" to "plot".
It's no longer a contingency table in fact.
I've seen in the mailing list archives that there is an issue on using
"table"s when matrixes or other structures would be more appropriate.
I like the "table", because "plot" automatically plots the
corresponding years on the x-axis, which I find less error-prone than
adding the tick labels later by hand, i.e. the association between
years and counts is stronger.
Also, as I tabulate counts of cases per gender, or per age categories,
I think a contingency table is the right thing to use, isn't it?
I'd be glad on any advice about what would be the best data structure
to handle my situation, and I'd also like to know how I could
automagically check a list of "year, cases" rows against a fixed list
of years, insert zero or "NA" values for missing years, and get an
easily usable result that I can forward to "plot" or "barplot".
Thanks a lot in advance,
Anne-Marie
More information about the R-help
mailing list