On Sep 9, 2015, at 11:52 AM, Nick Petschek wrote:
> Hi,
>
> I want to run frequency tables for multiple categorical variables, ideally
> one in %, and the other as a count, and am unsure how to proceed. I would
> like my output to be the following:
>
> | favorable | unfavorable | neutral
> Q1 | 80% | 10% | 10% |
> Q2 | 70% | 20% | 10% |
> Q3 | 60% | 10% | 30% |
>
> Then the same except counts instead of %.
>
> I've been looking into the table function, but can't quite get it
to work.
> Ideally I would love to export a large table to .csv.
dat <- read.delim(text=" | favorable | unfavorable | neutral
Q1 | 80% | 10% | 10%
Q2 | 70% | 20% | 10%
Q3 | 60% | 10% | 30%
", sep="|", strip.white=TRUE, stringsAsFactors=FALSE)
sim <- data.frame(
Q1 = sample(names(dat)[-1], 100, rep=TRUE,
prob=as.numeric(sub("%","", dat[-1][dat$X=="Q1",
]))/100),
Q2 = sample(names(dat)[-1], 100, rep=TRUE,
prob=as.numeric(sub("%","", dat[-1][dat$X=="Q2",
]))/100),
Q3 = sample(names(dat)[-1], 100, rep=TRUE,
prob=as.numeric(sub("%","", dat[-1][dat$X=="Q3",
]))/100),
stringsAsFactors=FALSE)
Looks to me that you want to read the help pages:
?table
?proptable
> sapply(sim,table) # trivially give percentages but wouldn't if the row
N's were not 100.
Q1 Q2 Q3
favorable 85 76 55
neutral 4 9 37
unfavorable 11 15 8
> prop.table( sapply(sim,table), 2)
Q1 Q2 Q3
favorable 0.85 0.76 0.55
neutral 0.04 0.09 0.37
unfavorable 0.11 0.15 0.08
And if you need them in row-acroos mode learn to use the `t`-function:
> 100* t( prop.table( sapply(sim,table), 2) )
favorable neutral unfavorable
Q1 85 4 11
Q2 76 9 15
Q3 55 37 8
You also need to read "An Introduction to R" and learn to post
examples in code.
> Any direction would be great!
>
> [[alternative HTML version deleted]]
And you need to read the Posting Guide and configure your mail-client to
>
> ______________________________________________
> R-help at 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.
David Winsemius
Alameda, CA, USA