2

I have a dataset with 2 columns as shown. What I want to do is plot a table summary with rows as groups (1-5, 6-10, 11-15, 16-20) and the columns as the % of yes and no. Don't have the code as I don't know how to start. But I have the dataset.

Col 1   Col 2
 1       Yes
 2       No
 3       Yes
 4       No
 5       No
 6       No
 7       Yes
 8       Yes
 9       Yes
10       No
12       Yes
14       No
16       No
18       No
20       Yes
2
  • I want the first column of the summary as groups 1-5, 6-10, .... the second column as Yes and the third column as No. Under Yes and No, I want the % of Yess and Nos. Commented Sep 30, 2018 at 20:03
  • No. Not a duplicate. The link shows a different example. Commented Sep 30, 2018 at 20:04

2 Answers 2

1

A base R solution could envolve cut and aggregate.

f <- cut(dat$Col.1, c(1, 6, 11, 16, 21, Inf),
         include.lowest = TRUE, right = FALSE)
agg <- aggregate(Col.2 ~ f, dat, table)
agg <- cbind(agg[1], agg[[2]])
agg[2:3] <- 100*agg[2:3]/rowSums(agg[2:3])

agg
#        f       No      Yes
#1   [1,6) 60.00000 40.00000
#2  [6,11) 40.00000 60.00000
#3 [11,16) 50.00000 50.00000
#4 [16,21) 66.66667 33.33333

Data.

dat <-
structure(list(Col.1 = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 12L, 14L, 16L, 18L, 20L), Col.2 = structure(c(2L, 1L, 2L, 
1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L), .Label = c("No", 
"Yes"), class = "factor")), class = "data.frame", row.names = c(NA, 
-15L))
Sign up to request clarification or add additional context in comments.

Comments

0

1) Using only base R and DF from the Note at the end:

nr <- nrow(DF)
group <- 5 * ceiling(DF$Col1 / 5)
100 * prop.table(xtabs(~ group + Col2, DF), 1)

giving:

     Col2
group       No      Yes
   5  60.00000 40.00000
   10 40.00000 60.00000
   15 50.00000 50.00000
   20 66.66667 33.33333

2) or with fancier group labels try this giving nicer labels than some other methods:

nr <- nrow(DF)

g <- 5 * ceiling(DF$Col1 / 5) # from above
labels <- unique(paste0(g-4, "-", g))
group <- factor(g, labels = labels)

tab <- 100 * prop.table(xtabs(~ group + Col2, DF), 1)

giving:

> tab
       Col2
group         No      Yes
  1-5   60.00000 40.00000
  6-10  40.00000 60.00000
  11-15 50.00000 50.00000
  16-20 66.66667 33.33333

> # another layout
> ftable(tab, row.vars = 1:2)

group Col2          
1-5   No    60.00000
      Yes   40.00000
6-10  No    40.00000
      Yes   60.00000
11-15 No    50.00000
      Yes   50.00000
16-20 No    66.66667
      Yes   33.33333

> plot(tab, col = c("pink", "lightgreen"))

screenshot

Note

The input DF in reproducible form is:

Lines <- "Col1   Col2
 1       Yes
 2       No
 3       Yes
 4       No
 5       No
 6       No
 7       Yes
 8       Yes
 9       Yes
10       No
12       Yes
14       No
16       No
18       No
20       Yes"
DF <- read.table(text = Lines, header = TRUE)

Update

Revised several times.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.