R根据行值将索引列添加到数据框 [英] R add index column to data frame based on row values

查看:2082
本文介绍了R根据行值将索引列添加到数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 r - 如何根据因素组合向数据框添加行索引

我试图复制我认为的内容使用绿色检查答案是理想的结果,我一直得到一些不同于预期的东西。我确信我正在做一些非常基本的错误,但似乎无法看到它或者我误解了所期望的状态。

I tried to replicate what I believe to be the desired results using the green checked answer and am consistently getting something other than expected. I am sure I am doing something really basic wrong, but can't seem to see it OR I've misunderstood what the desired state is.

原始数据发布:

temp <- data.frame(
Dim1 = c("A","A","A","A","A","A","B","B"),
Dim2 = c(100,100,100,100,200,200,100,200),
 Value = sample(1:10, 8)
 )

然后我运行以下代码: temp $ indexLength < - ave(1:nrow(temp),temp $ Dim1,factor(temp $ Dim2),FUN = function(x)1:length(x))

Then I ran the following code: temp$indexLength <- ave( 1:nrow(temp), temp$Dim1, factor( temp$Dim2), FUN=function(x) 1:length(x) )

和: temp $ indexSeqAlong< - ave(1:nrow(temp),temp $ Dim1,factor(temp $ Dim2),FUN = seq_along)

然后我创建了以下内容: temp $ indexDesired< - c(1,1,1,1,2,2 ,3,3)

and then I created the following: temp$indexDesired <- c(1, 1, 1, 1, 2, 2, 3, 3)

...以下面的数据框结束:

...ending up with the data frame below:

  Dim1 Dim2 Value indexLength indexSeqAlong indexDesired
1    A  100     6           1             1            1
2    A  100     2           2             2            1
3    A  100     9           3             3            1
4    A  100     8           4             4            1
5    A  200    10           1             1            2
6    A  200     4           2             2            2
7    B  100     3           1             1            3
8    B  200     5           1             1            4

如果我能弄清楚我没有得到所需的索引 - 并假设代码可扩展到超过2个变量 - 我应该全部设置。提前致谢!

If I can figure out what I'm not getting the desired index -- and assuming the code is extensible to more than 2 variables -- I should be all set. Thanks in advance!

推荐答案

如果您使用 data.table ,那里是一个符号 .GRP ,它记录了这些信息(一个简单的组计数器)

If you use data.table, there is a "symbol" .GRP which records this information ( a simple group counter)

library(data.table)
DT <- data.table(temp)
DT[, index := .GRP, by = list(Dim1, Dim2)]
DT
#    Dim1 Dim2 Value index
# 1:    A  100    10     1
# 2:    A  100     2     1
# 3:    A  100     9     1
# 4:    A  100     4     1
# 5:    A  200     6     2
# 6:    A  200     1     2
# 7:    B  100     8     3
# 8:    B  200     7     4

这篇关于R根据行值将索引列添加到数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆