在R中订购数据帧

时间:2013-12-31 17:13:56

标签: r sorting dataframe plyr

我有以下数据框架结构:

    Animal          Food
1    cat      fish, milk, shrimp
2    dog      steak, poo
3    fish     seaweed, shrimp, krill, insects

我想重新组织它,以便行在“食物”列中按因子数的降序排列:

    Animal          Food
1    fish     seaweed, shrimp, krill, insects
2    cat      fish, milk, shrimp
3    dog      steak, poo

是否有R功能可以帮助我? 感谢

3 个答案:

答案 0 :(得分:4)

您可以使用count.fields来确定每个“食物”行中的项目数量和顺序。

count.fields(textConnection(mydf$Food), ",")
# [1] 3 2 4

假设您的data.frame被称为“mydf”:

mydf[order(count.fields(textConnection(mydf$Food), ","), decreasing=TRUE),]
#   Animal                            Food
# 3   fish seaweed, shrimp, krill, insects
# 1    cat              fish, milk, shrimp
# 2    dog                      steak, poo

答案 1 :(得分:1)

创建一个新变量并按其排序,编辑:感谢Ananda和alexis

<德尔> df$nFood<-length(unlist(strsplit(df$Food, ",", fixed=T)))

df$nFood<-sapply(strsplit(df$Food, ","), length)

答案 2 :(得分:1)

您可以根据计数功能的结果订购相框:

animals = data.frame( rbind(c("cat","fish, milk, shrimp"),
                  c("dog","steak, poo"),
                  c("fish","seaweed, shrimp, krill, insects")))
colnames(animals) = c("Animal","Food")
animals[order(sapply(animals$Food, function(x) { length(strsplit(as.character(x),split=",")[[1]]) })), ]

我输入了as.character,因为它默认为一个因素,您可能不需要它(更快),或者您可以在创建数据框时使用stringsAsFactors=FALSE