[R] Banner using R

David L Carlson dc@r|@on @end|ng |rom t@mu@edu
Tue Jan 15 19:45:01 CET 2019


You need to read the data without converting to factors:

> Data <- data.frame(v1 = c('A', 'B' ,'B' ,'A', 'B'), v2 =c('A', 'B', 'A', 'A', 'B'), 
+      v3 = c('A', 'A', 'A', 'A', 'A'), stringsAsFactors=FALSE)

Then you need to stack it:

> Data.stack <- stack(Data)
> str(Data.stack)
'data.frame':   15 obs. of  2 variables:
 $ values: chr  "A" "B" "B" "A" ...
 $ ind   : Factor w/ 3 levels "v1","v2","v3": 1 1 1 1 1 2 2 2 2 2 ...

Then table (or xtabs) will work:

> Data.tbl <- table(Data.stack)
> addmargins(Data.tbl, 2)
      ind
values v1 v2 v3 Sum
     A  2  3  5  10
     B  3  2  0   5

----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Luca Meyer
Sent: Tuesday, January 15, 2019 12:26 PM
To: r-help using r-project.org
Subject: [R] Banner using R

Hi,

I am a bit rusty with R programming and I would appreciate some assistance with the following.

I have a dataset like:

Data <- data.frame(v1 = c('A', 'B' ,'B' ,'A', 'B'), v2 =c('A', 'B', 'A', 'A', 'B'), v3 = c('A', 'A', 'A', 'A', 'A’))

How can I get a banner of the sort?

Count	v1	v2	v3	TOT
A		2	3	5	10
B		3	2	0	5

I have tried with xtabs and expss but I do not seem to get what I need...

Thanks,

Luca

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
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