创建描述特定值的现有索引位置的新变量

时间:2022-05-14 21:25:22

I have a data frame in R with a column containing a unique ID and 10 variables which looks like this:

我在R中有一个数据框,列中包含唯一ID和10个变量,如下所示:

    id V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1  555  2  1 10  3  8  9  5  7  4   6
2  681 10  5  4  8  7  3  6  2  9   1
3 2300  8  5 10  2  3  1  6  9  4   7

I want to create variables named or1 through or10 which capture the column position of the values 1:10 for each row.

我想创建一个名为or1到or10的变量,它为每一行捕获值1:10的列位置。

So, for example, in the first row, the number 1 appears in the column named V2, so or1 = 2. If I managed to do this correctly for the three rows above, I'd get the following:

例如,在第一行中,数字1出现在名为V2的列中,所以or1 = 2。如果我能正确地完成上面三行,我会得到以下结果:

    id or1 or2 or3 or4 or5 or6 or7 or8 or9 or10
1  555  2   1   4   9   7  10   8   5   6    3
2  681 10   8   6   3   2   7   5   4   9    1
3 2300  6   4   5   9   2   7  10   1   8    3  

I managed to create or1 using an absurdly long set of if/then statements but I know there is a much better way that probably involves indexing. All help is greatly appreciated.

我使用了一组长得荒谬的if/then语句创建了or1,但我知道有一种更好的方法可能涉及索引。非常感谢大家的帮助。

1 个解决方案

#1


4  

This does it:

这它:

> ord <- data.frame(id = dat$id, t(apply(dat[, -1], 1, order)))
    id X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1  555  2  1  4  9  7 10  8  5  6   3
2  681 10  8  6  3  2  7  5  4  9   1
3 2300  6  4  5  9  2  7 10  1  8   3

(In case "id" is not the first column, I would replace dat[, -1] with the more robust dat[!colnames(dat) %in% "id"])

(如果“id”不是第一列,我将用更健壮的dat[!colnames(dat) % In“id”)替换dat[, -1])

Then, to get the column names exactly like you wanted:

然后,要得到您想要的列名:

colnames(ord) <- sub("^X", "or", colnames(ord))

#1


4  

This does it:

这它:

> ord <- data.frame(id = dat$id, t(apply(dat[, -1], 1, order)))
    id X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1  555  2  1  4  9  7 10  8  5  6   3
2  681 10  8  6  3  2  7  5  4  9   1
3 2300  6  4  5  9  2  7 10  1  8   3

(In case "id" is not the first column, I would replace dat[, -1] with the more robust dat[!colnames(dat) %in% "id"])

(如果“id”不是第一列,我将用更健壮的dat[!colnames(dat) % In“id”)替换dat[, -1])

Then, to get the column names exactly like you wanted:

然后,要得到您想要的列名:

colnames(ord) <- sub("^X", "or", colnames(ord))