【发布时间】:2018-07-26 08:10:23
【问题描述】:
我有两个列表,每个列表包含 37 个项目(我在这里以 3 个为例):
vacancy.locations <- c("Amsterdam", "Zuid Holland", "Utrecht")
count.locations <- c("11", "9", "40")
我将这两个列表绑定在一起locations <- cbind(vacancy.locations, count.locations,这样我就可以按降序排序sortedlocations <- locations[order(-count.locations),],而不会丢失11 属于阿姆斯特丹和40 属于乌得勒支的事实。
但是,现在我只想保留数量最多的 10 个位置。谁能帮我做到这一点?
在此之后,我想在条形图中绘制前 10 个位置。目前我正在尝试使用 sortedlocations,但是我在图表中只得到 1 个条形图,所有位置都结合在一起。
barplotLocations <- barplot(height=sortedlocations, las=2, main="locations in vacancies", xlab="locations", ylab="number", cex.axis = .5, cex.names = .75)
帮助? :)
【问题讨论】:
-
嘿,在这种情况下使用
c()会给你一个vector。您可以使用list()获得一个列表。 -
1.order
count.locationsdesc。 2。使用df[1:10,]或head(df,10)
标签: r sorting bar-chart reverse cbind