[R] How to add elements to a table

Jacques VESLOT Jacques.Veslot at avignon.inra.fr
Fri Jun 29 11:11:47 CEST 2007


 > z <- read.table("clipboard")
 > z
    V1 V2
1 1990 20
2 1991 15
3 1995 17
4 1997  9
5 1998 10
6 1999 11
7 2000  5
 > zz <- merge(data.frame(V1=1990:2000), z, by="V1", all.x=T)
 > plot(zz, type="l")

Jacques VESLOT

INRA - Biostatistique & Processus Spatiaux
Site Agroparc 84914 Avignon Cedex 9, France

Tel: +33 (0) 4 32 72 21 58
Fax: +33 (0) 4 32 72 21 84



bioinfonews at pt.lu a écrit :
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list