如何在#34; UniqueCarrier"是一个角色,"延迟"包含0和1?
plot(Delay ~ UniqueCarrier, data = mydata)
答案 0 :(得分:3)
我们可以通过以下方式使用barplot
来显示字符变量的每个唯一值的1的计数
# Generate sample data
uniqueCarrier <- unlist(lapply(1:10, function(i) rep(paste(sample(letters,size = 3), collapse=""),10)))
Delay <- rbinom(100, 1, prob = rep(c(.30, .2, .1, .5, .7, .6, .9, .2, .7, .6),each = 10))
# Create the plot
barplot(by(data = Delay, INDICES = uniqueCarrier, sum), cex.names = 0.9)
生成数据
> dput(uniqueCarrier)
c("ekp", "ekp", "ekp", "ekp", "ekp", "ekp", "ekp", "ekp", "ekp",
"ekp", "vtq", "vtq", "vtq", "vtq", "vtq", "vtq", "vtq", "vtq",
"vtq", "vtq", "jic", "jic", "jic", "jic", "jic", "jic", "jic",
"jic", "jic", "jic", "sab", "sab", "sab", "sab", "sab", "sab",
"sab", "sab", "sab", "sab", "ekl", "ekl", "ekl", "ekl", "ekl",
"ekl", "ekl", "ekl", "ekl", "ekl", "ifx", "ifx", "ifx", "ifx",
"ifx", "ifx", "ifx", "ifx", "ifx", "ifx", "miw", "miw", "miw",
"miw", "miw", "miw", "miw", "miw", "miw", "miw", "oif", "oif",
"oif", "oif", "oif", "oif", "oif", "oif", "oif", "oif", "qto",
"qto", "qto", "qto", "qto", "qto", "qto", "qto", "qto", "qto",
"vqw", "vqw", "vqw", "vqw", "vqw", "vqw", "vqw", "vqw", "vqw",
"vqw")
> dput(Delay)
c(1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L,
1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L,
0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L,
1L, 1L, 0L, 1L, 0L)