frequency - How to create missing values in table in R? -


i have 40 pairs of birds each male , female in pair scored colour. colour score categorical variable value range of 1 9. create table number of each combination (1/1, 1/2, 1/3, ... 9/7, 9/8, 9/9). problem there combinations not exist in data when try create table (in these cases zeros missing values). below data , sample code. pretty sure answer lies in using 'expand.grid()' command, e.g. see post, unsure how implement it. suggestions?

## dataset pairs of males , females , colour classes pair_colours <- structure(list(male = c(7, 6, 4, 6, 8, 8, 5, 6, 6, 8, 6, 6, 5,  7, 9, 5, 8, 7, 5, 5, 4, 6, 7, 7, 3, 6, 5, 4, 7, 4, 3, 9, 4, 4,  4, 4, 9, 6, 6, 6), female = c(9, 8, 8, 9, 3, 6, 8, 5, 8, 9, 7,  3, 6, 5, 8, 9, 7, 3, 6, 4, 4, 4, 8, 8, 6, 7, 4, 2, 8, 9, 5, 6,  8, 8, 4, 4, 5, 9, 7, 8)), .names = c("male", "female"), class = "data.frame", row.names = c(na,  40l))  pair_colours$male <- as.factor(pair_colours$male) pair_colours$female <- as.factor(pair_colours$female)  ## table of pair colour values (colours 1 9 - categoricial variable) table(pair_colours$male, pair_colours$female)  ## attempt create table count of each possible value pairs colour_male <- rep(seq(1, 9, = 1), each = 9) colour_female <- rep(seq(1, 9, = 1), times = 9) colour_count <-  as.vector(table(pair_colours$male, pair_colours$female)) # <- problem occurs here pairs_colour_table <- as.data.frame(cbind(cbind(colour_male, colour_female), colour_count))  ## plot results visisually possible assortative mating colour op<-par(mfrow=c(1,1), oma=c(2,4,0,0), mar=c(4,5,1,2), pty = "s") plot(1,1, xlim = c(1, 9), ylim = c(1, 9), type="n", xaxt = "n", yaxt = "n", las=1, bty="n", cex.lab = 1.75, cex.axis = 1.5, main = null, xlab = "male colour", ylab = "female colour", pty = "s") axis(1, @ = seq(1, 9, = 1), labels = t, cex.lab = 1.5, cex.axis = 1.5, tick = true, tck = -0.015, lwd = 1.25, lwd.ticks = 1.25) axis(2, @ = seq(1, 9, = 1), labels = t, cex.lab = 1.5, cex.axis = 1.5, tick = true, tck = -0.015, lwd = 1.25, lwd.ticks = 1.25, las =2) points(pair_colours$male, pair_colours$female, pch = 21, cex = pairs_colour_table$colour_count, bg = "darkgray", col = "black", lwd = 1) 

you have convert pair_colours factor required levels before calling table:

# convert each column factor levels 1 9 pair_colours[] <- lapply(pair_colours, factor, levels=1:9) table(pair_colours$male, pair_colours$female) #     1 2 3 4 5 6 7 8 9 #   1 0 0 0 0 0 0 0 0 0 #   2 0 0 0 0 0 0 0 0 0 #   3 0 0 0 0 1 1 0 0 0 #   4 0 1 0 3 0 0 0 3 1 #   5 0 0 0 2 0 2 0 1 1 #   6 0 0 1 1 1 0 3 3 2 #   7 0 0 1 0 1 0 0 3 1 #   8 0 0 1 0 0 1 1 0 1 #   9 0 0 0 0 1 1 0 1 0 

you can convert as.data.frame if want format "combn1, combn2, frequency".


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -