English 中文(简体)
创建新列,根据前一列排列数据框架顺序
原标题:create a new column that arrange data frame order based on previous columns
  • 时间:2012-05-26 11:30:39
  •  标签:
  • r

我有一个像这样的数据框 :

  A     B      C    
1   1   1 0.4519
2 101   1 0.3819
3 201   1 0.3819
4 301   1 0.2819
5 401   1 0.9819
6 501   1 0.6819

范围更大,但举个例子。

i 想要创建一个名为 order 的新列,其中包括从 (1至nrow(df)) (1至nrow(drow)) > (1至ncode)的数,并且根据 C 列的值而增加。 (1为最小值,增加值随着 C 值的增加而增加)。 当C 列中的值等值将顺序标准更改为 A/code> 列时,当A列中的值相等时,将其更改为 B/code> 列。

能否在R语中以简单、有效的方式做到这一点?

可以通过数据框架的环环循环来完成, 如果声明, 也可以做出一些声明, 但是完成它需要花费很多时间 。 这就是为什么我需要一个更快的替代品, 如果可能的话 。

感谢您,感谢您

问题回答

您可以使用 order 来排序基于多个列的数据。 根据 order 文档:

In the case of ties in the first vector, values in the second are used to break the ties. If the values are still tied, values in the later arguments are used to break the tie.

这将是你的样本数据框:

df <- data.frame(list(A=c(1, 101, 201, 301, 401, 501),
                      B=rep(1, 6),
                      C=c(0.45, 0.38, 0.38, 0.28, 0.98, 0.68)))

这是根据您想要的顺序在数据框中创建新列的代码 :

df$order <- order(df$C, df$A, df$B)




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...