[R] import contingency table
David L Carlson
dcarlson at tamu.edu
Mon May 28 17:31:09 CEST 2012
How about this?
exdf <- read.table("clipboard", sep=",", header=T, row.names=1)
extbl <- as.table(as.matrix(exdf))
----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Rui Barradas
> Sent: Monday, May 28, 2012 9:43 AM
> To: Milan Bouchet-Valat
> Cc: r-help
> Subject: Re: [R] import contingency table
>
> Ok, try the following.
>
>
> df2table <- function(x, Var1="Var1", Var2="Var2"){
> tbl <- as.matrix(x)
> dnames <- list(rownames(x), colnames(x))
> names(dnames) <- c(Var1, Var2)
> attr(tbl, "dimnames") <- dnames
> attr(tbl, "class") <- "table"
> tbl
> }
>
> df2table(xls_contingency) # using default names
>
> Rui Barradas
>
>
> Em 28-05-2012 15:00, Milan Bouchet-Valat escreveu:
> > Le lundi 28 mai 2012 à 15:19 +0200, sylvain willart a écrit :
> >> no,
> >> the problem is that the lines in my file do not correspond to
> >> individuals, but are variables, just like are the columns,
> >> my file is already a contingency table, with each cell being a
> frequency:
> >>
> >> here is a sample of it:
> >> *******************************************************
> >>
> ,AUC,Alin,BLG,BrDep,CRF,CMkt,CAS,Casto,Confo,ElDep,Geant,Halle,KIA,LMrl
> ,Match,MET,MNP,SM,
> >> Strasbg,4,0,0,2,3,0,0,6,2,1,2,1,0,2,3,2,3,6
> >> Paris,0,0,0,0,10,1,5,2,4,0,5,1,0,0,0,3,7,7
> >> Brest,3,0,0,2,8,0,5,9,4,0,5,0,2,0,0,0,0,0
> >> Lyon,0,0,0,1,4,2,8,2,3,0,5,1,0,0,0,0,4,5
> >> Nice,3,0,0,0,3,2,5,1,2,0,2,0,0,0,0,2,2,0
> >> Limg,3,0,0,1,4,2,3,0,0,0,3,0,0,0,0,1,0,4
> >> Toulse,0,0,0,1,5,4,3,2,2,0,5,0,0,0,0,2,1,5
> >> Nancy,0,0,0,2,3,1,1,8,2,0,2,0,1,0,2,3,2,4
> >> Lille,0,0,0,0,6,8,0,0,2,2,3,1,0,1,5,1,2,6
> >> Mtplier,0,0,0,0,7,3,4,1,0,1,4,0,0,0,0,1,6,3
> >> Aix,0,4,0,0,9,2,5,1,0,0,5,0,0,0,0,1,7,5
> >> Senart,0,0,0,1,10,3,5,0,5,0,6,0,0,0,0,0,3,3
> >> Grenbl,0,0,0,0,3,2,5,3,1,0,5,0,0,0,0,0,0,4
> >> Angers,0,0,0,2,8,0,4,0,4,0,4,0,2,0,0,0,3,3
> >> Brdx,3,0,0,2,4,3,3,0,1,0,5,0,2,0,0,1,3,4
> >> Dijon,0,0,0,1,8,2,5,3,4,0,5,0,0,0,0,2,1,0
> >> Rouen,3,0,0,1,2,0,2,0,3,1,2,1,2,0,0,0,0,6
> >> **********************************************************
> >>
> >> I know how to read it into a df or a matrix,
> >> if it was a df or matrix, i could turn it into a table,
> >> but this is already a contingency table
> > If it's already a matrix, just call as.table() on it, and you'll get
> a
> > table object.
> >
> >> for example, the first number "4", is the number of people being in
> >> city "Strasbg" (first row) and working at "AUC" (first column) (this
> >> is Auchan actually)
> >>
> >> I do not have the original file where each row would be an
> individual,
> >> I just have that flat file, with variables on the rows and variables
> >> on the colums, and frequencies in each cell,
> >> And I wonder how to read it in R telling him this is a
> >> frequency/contingency table ....
> >>
> >> I can't believe there are no way of getting aroud it (or maybe the
> sun
> >> stroke to heavy on my head) ....
> > Please call dput() on the data as you have imported it, so that we
> can
> > precisely discuss the problem.
> >
> >
> > Regards
> >
> > ______________________________________________
> > R-help at r-project.org 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.
>
> ______________________________________________
> R-help at r-project.org 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