[R] Data frame/read.ftable

David Winsemius dwinsemius at comcast.net
Thu Dec 3 03:29:37 CET 2009


On Dec 2, 2009, at 7:02 PM, Robinson, David G wrote:

> My apologies for this question but I¹m stuck and I¹m sure that there  
> must be
> an easy answer out there (and hope that someone will have mercy and  
> point me
> in the right direction).
>
> I have a data file that looks like:
> 1 77 3
> 1 8 1
> 1 7 2
> 1 1 5
> 1 42 7
> 1 0 2
> 1 23 1
> 2 83 9
> 2 8 2
> 2 6 5
> 2 23 3
> 3 11 3
> 3 8 1
> .
> .... etc.
> .
> N   3   2
>
>
> (FWIW, these are document, word reference, and word frequency  
> counts.) I
> want to read the data into data frame, Doc, such that
> Doc[[1]]=
>     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
> [1,]    77    8    7    1    42    0    23
> [2,]    3    1    2    5     7    2     1
>
> Doc[[2]]=
>     [,1] [,2] [,3] [,4]
> [1,]    83    8    6    23
> [2,]    9    2    5    3
>
> Etc.

rd.txt <- function(txt, header=TRUE) {read.table(textConnection(txt),  
header=header)}

 > dta <- rd.txt("1 77 3
+ 1 8 1
+ 1 7 2
+ 1 1 5
+ 1 42 7
+ 1 0 2
+ 1 23 1
+ 2 83 9
+ 2 8 2
+ 2 6 5
+ 2 23 3
+ 3 11 3
+ 3 8 1", header=F)
 > dta
    V1 V2 V3
1   1 77  3
2   1  8  1
3   1  7  2
4   1  1  5
5   1 42  7
6   1  0  2
7   1 23  1
8   2 83  9
9   2  8  2
10  2  6  5
11  2 23  3
12  3 11  3
13  3  8  1


 > split(dta[ ,-1], list(dta[,1]))
$`1`
   V2 V3
1 77  3
2  8  1
3  7  2
4  1  5
5 42  7
6  0  2
7 23  1

$`2`
    V2 V3
8  83  9
9   8  2
10  6  5
11 23  3

$`3`
    V2 V3
12 11  3
13  8  1

 > ?split
 > lapply(split(dta[ ,-1], list(dta[,1])), t)
$`1`
     1 2 3 4  5 6  7
V2 77 8 7 1 42 0 23
V3  3 1 2 5  7 2  1

$`2`
     8 9 10 11
V2 83 8  6 23
V3  9 2  5  3

$`3`
    12 13
V2 11  8
V3  3  1

>
>
> It seems like I should be able to do this using a flat contingency  
> table
> method such as 'read.ftable' or possibly using 'stack' . However,  
> something
> is not clicking and hence my plea for assistance.
>
> Thanks in advance,
> Dave Robinson
> drobin at sandia.gov
>
> ______________________________________________
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list