IRNotSmart
IRNotSmart

Reputation: 371

How can you create custom headers using Table function in R?

I have a data frame for each team that looks like nebraska below. However, some of these poor teams don't have a single win, so their $Outcome column has nothing but L in them.

> nebraska
           Teams     Away/Home   Score   Outcome
1     Arkansas State      Away    36 
2           Nebraska      Home    43        W
3           Nebraska      Away    35        L
4             Oregon      Home    42                          
5  Northern Illinois      Away    21                          
6           Nebraska      Home    17        L
7            Rutgers      Away    17                          
8           Nebraska      Home    27        W
9           Nebraska      Away    28        W
10          Illinois      Home     6                          
11         Wisconsin      Away    38                          
12          Nebraska      Home    17        L
13        Ohio State      Away    56                          
14          Nebraska      Home    14        L

When I run table(nebraska$Outcome it gives me my expected outcome:

table(nebraska$Outcome)
  L W 
7 4 3 

However, for the teams that don't have a single win (like Baylor), or only have wins, it only gives me something like:

table(baylor$Outcome)
  L
7 7 

I'd like to specify custom headers for the table function so that I can get have something like this output:

table(baylor$Outcome)
  L W
7 7 0

I've tried passing the argument dnn to the table function call, but it throws an error with the following code:

> table(baylor$Outcome,dnn = c("W","L",""))

Error in names(dn) <- dnn : 'names' attribute [3] must be the same length as the vector [1]

Can someone tell me how I can tabulate these wins and losses correctly?

Upvotes: 1

Views: 781

Answers (3)

thelatemail
thelatemail

Reputation: 93908

I don't think this has to be that complicated. Just make baylor$Outcome a factor and then table. E.g.:

# example data
baylor <- data.frame(Outcome = c("L","L","L"))

Then it is just:

baylor$Outcome <- factor(baylor$Outcome, levels=c("","L","W"))
table(baylor$Outcome)

#  L W 
#0 3 0 

Upvotes: 2

Kevin Arseneau
Kevin Arseneau

Reputation: 6264

Following a tidy workflow, I offer...

library(dplyr)
library(tidyr)

df <- nebraska %>%
  group_by(Teams, Outcome) %>%
  summarise(n = n()) %>%
  spread(Outcome, n) %>%
  select(-c(`<NA>`))

# # A tibble: 8 x 3
# # Groups:   Teams [8]
#               Teams     L     W
# *             <chr> <int> <int>
# 1    Arkansas State    NA    NA
# 2          Illinois    NA    NA
# 3          Nebraska     4     3
# 4 Northern Illinois    NA    NA
# 5        Ohio State    NA    NA
# 6            Oregon    NA    NA
# 7           Rutgers    NA    NA
# 8         Wisconsin    NA    NA

...and I couldn't help myself but to pretty with knitr::kable and kableExtra

library(knitr)
library(kableExtra)

df %>%
  kable("html") %>%
  kable_styling(bootstrap_options = c("striped", "hover"))

enter image description here

Upvotes: 1

d.b
d.b

Reputation: 32548

Try this:

with(rle(sort(nebraska$Outcome)),
     data.frame(W = max(0, lengths[values == "W"]),
                L = max(0, lengths[values == "L"])))
#  W L
#1 3 4

Upvotes: 2

Related Questions