[R] import contingency table
sylvain willart
sylvain.willart at gmail.com
Mon May 28 15:19:11 CEST 2012
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
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) ....
Sylv
2012/5/28 Nicolas Iderhoff <nicolasiderhoff at googlemail.com>:
> Wouldn't it work for you to read the data into a matrix/df so you can transform it into a table()?
> if you're worried about the names of the cols/rows, you can always do
> read.table(..)[,1] to get the row names for example and put them into the matrix with rownames()
>
> Am 28.05.2012 um 13:49 schrieb sylvain willart:
>
>> there are no indication in ?table on how to read in a contingency
>> table (only on how to transform a dataframe or matrix into a
>> contingency table),
>> when I read my file with read.table(), and run is.table() I get
>> "FALSE" for an answer, and the function as.table() leads to an error
>> message,
>> Sylv
>>
>>
>> 2012/5/28 Nicolas Iderhoff <nicolasiderhoff at googlemail.com>:
>>> Try
>>>
>>> ?table
>>>
>>>
>>> Am 28.05.2012 um 13:33 schrieb sylvain willart:
>>>
>>>> Thanks Rui,
>>>> but my problem is not to read an xls file, I converted already to csv,
>>>> but rather to read a contingency table into R, and telling R it is
>>>> astually a contingency table, and not a data.frame...
>>>>
>>>> file below, if it helps...
>>>>
>>>> Sylv
>>>>
>>>> ,AUC,Alin,BLG,BrDep,CRF,CrfMkt,CAS,Casto,Confo,ElecDep,Geant,Halle,KIA,LerMrl,Match,METRO,MNP,SimpMkt
>>>> 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
>>>>
>>>> 2012/5/28 Rui Barradas <ruipbarradas at sapo.pt>:
>>>>> Hello,
>>>>>
>>>>> Try function read.xls in library gdata
>>>>> Also, a good way of avoiding such doubts is
>>>>>
>>>>> library(sos)
>>>>> findFn('xls')
>>>>>
>>>>> It returns read.xls as the first line.
>>>>>
>>>>> Hope this helps,
>>>>>
>>>>> Rui Barradas
>>>>>
>>>>> Em 28-05-2012 11:32, sylvain willart escreveu:
>>>>>>
>>>>>> hello everyone,
>>>>>>
>>>>>> i often work on contingency table that I create from data.frame (with
>>>>>> table() function)
>>>>>>
>>>>>> but a friend sent me an excel sheet wich *already is* a contingency
>>>>>> table (just a simple 2 way table !...)
>>>>>>
>>>>>> any clue on how to import it in R (keeping row names and col names) ?
>>>>>>
>>>>>> any tuto I come accross only mention the table transformation, but
>>>>>> never the import of such data
>>>>>>
>>>>>> I only found read.ftable() but couldn't get it to work
>>>>>>
>>>>>> any help appreciated
>>>>>>
>>>>>> Sylv
>>>>>>
>>>>>> ______________________________________________
>>>>>> 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