简体   繁体   English

从R数据框中提取和排序特定值

[英]Extract and sort specific values from an R data frame

I´m quite new to R, and this might there be an easy answer to, but still: I have a data frame on the form 我是R的新手,可能对此有一个简单的答案,但仍然:我在表单上有一个数据框

df <- data.frame(c("a", "b", "c", "d", "e"), 1:5, 7:11, stringsAsFactors=FALSE)
names(df) <- c("en", "to", "tre")

My dataset is way bigger than this, lots more rows and columns. 我的数据集比这更大,行和列更多。 But the basic idea is the same: I want to sort the n highest numeric values, independent of which column they appear in, and return a list with the values in decreasing order and their corresponding string in column "en". 但是基本思想是相同的:我想对n个最大的数值进行排序,而与它们出现在哪一列无关,并返回一个列表,其中值按降序排列,并且在“ en”列中包含相应的字符串。

Like this: 像这样:

e  11
d  10
c   9
b   8
a   7
e   5

and so on. 等等。

How could I go about accomplishing this? 我该怎么做呢?

You can use the package reshape2 to melt your data and sort the value column, like this : 您可以使用reshape2包来融化数据并对value列进行排序,如下所示:

require(reshape2)
df <- data.frame(c("a", "b", "c", "d", "e"), 1:5, 7:11, stringsAsFactors=FALSE)
names(df) <- c("en", "to", "tre")

df2 <- melt(df, id = "en")
## 'data.frame':    10 obs. of  3 variables:
##  $ en      : chr  "a" "b" "c" "d" ...
##  $ variable: Factor w/ 2 levels "to","tre": 1 1 1 1 1 2 2 2 2 2
##  $ value   : int  1 2 3 4 5 7 8 9 10 11

df2[order(df2$value, decreasing = TRUE), c("en", "value")]
##    en value
## 10  e    11
## 9   d    10
## 8   c     9
## 7   b     8
## 6   a     7
## 5   e     5
## 4   d     4
## 3   c     3
## 2   b     2
## 1   a     1

But I'm sure there are other ways to do that !! 但是我敢肯定还有其他方法可以做到这一点!

Less elegant but without extra packages (it will work with any number of columns): 不太优雅,但没有额外的程序包(它将与任何数量的列一起使用):

col1<-rep(df[,1],ncol(df)-1)
col2<-c()
for(i in 2:ncol(df)) {
    col2<-c(col2,df[,i])
}
newdf<-data.frame(en=col1,value=col2)
newdf<-newdf[order(as.numeric(newdf[,2]),decreasing=TRUE),]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM