Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 基于列值的重复排序_R_Indexing_Sequencing - Fatal编程技术网

R 基于列值的重复排序

R 基于列值的重复排序,r,indexing,sequencing,R,Indexing,Sequencing,我对R真的很陌生,所以如果我没有完全理解,我很抱歉 我有一个DF,其中包含在几个不同区域收集的数据,称为STAND。我需要为我的数据创建一个从1:3开始运行的序列,但在涉及到新的机架编号时,is必须重新启动该序列 下面是一些虚拟数据 STAND TREE_SPECIES DIAMETER 1 101737 Pine 276 2 101737 Spruce 98 3 101737 Spruce 104 4

我对R真的很陌生,所以如果我没有完全理解,我很抱歉

我有一个DF,其中包含在几个不同区域收集的数据,称为STAND。我需要为我的数据创建一个从1:3开始运行的序列,但在涉及到新的机架编号时,is必须重新启动该序列

下面是一些虚拟数据

    STAND   TREE_SPECIES    DIAMETER
1   101737  Pine             276
2   101737  Spruce           98
3   101737  Spruce       104
4   101737  Leaf         53
5   155897  Spruce       82
6   155897  Spruce       61
7   155897  Leaf         97
8   155897  Spruce       89
9   155897  Spruce       75
10  202568  Spruce       46
11  202568  Spruce       56
12  202568  Pine         204
13  202568  Spruce       132
14  202568  Spruce       93 
我希望它看起来像这样:

    STAND   TREE_SPECIES    DIAMETER     SEQ
1   101737  Pine             276          1
2   101737  Spruce           98           2
3   101737  Spruce       104          3
4   101737  Leaf         53           1
5   155897  Spruce       82           1
6   155897  Spruce       61           2
7   155897  Leaf         97           3
8   155897  Spruce       89           1
9   155897  Spruce       75           2
10  202568  Spruce       46           1
11  202568  Spruce       56           2
12  202568  Pine         204          3
13  202568  Spruce       132          1
14  202568  Spruce       93           2
如果有什么帮助的话,我的DF总共有7416行,分为90个看台

到目前为止,我已经尝试:

  myDF$SEQ <- seq(1:3)
myDF$SEQ试试这个:

df$SEQ <- ave(x = df$STAND, df$STAND, FUN = function(y) rep(1:3, length.out = length(y)))

ave
x
向量(此处:STAND)拆分为由下一个(未命名)参数(此处:STAND)的级别定义的片段。应用于
ave
中每个片段的默认函数
FUN
mean
。在这里,我们将该函数更改为“匿名函数”
function(y)
,我们将其定义为
rep(1:3,length.out=length(y))
。“y”对应于每个片段。您可以用任意选择的名称替换“y”(例如,
function(chunk)rep(1:3,length.out=length(chunk))
。您会发现人们经常使用
函数(x)
,但我不想在这里使用'x'作为每个片段的名称,因为'x'在整个向量的
ave
中也用作参数。对于每个片段,
rep
将值1:3复制到所需的长度(
length.out
),即每个片段的长度:
length(y)

大家好,欢迎来到stackoverflow!由于您是SO的新手,请花一些时间阅读和了解。如果您提供一个解决方案,并向我们展示您尝试的解决方案,为什么它们不起作用,以及预期的结果,您将更有可能收到答案。谢谢!
df$SEQ2 <- ave(df$STAND, df$STAND, FUN = function(y) 1:3)
df
#     STAND TREE_SPECIES DIAMETER SEQ SEQ2
# 1  101737         Pine      276   1    1
# 2  101737       Spruce       98   2    2
# 3  101737       Spruce      104   3    3
# 4  101737         Leaf       53   1    1
# 5  155897       Spruce       82   1    1
# 6  155897       Spruce       61   2    2
# 7  155897         Leaf       97   3    3
# 8  155897       Spruce       89   1    1
# 9  155897       Spruce       75   2    2
# 10 202568       Spruce       46   1    1
# 11 202568       Spruce       56   2    2
# 12 202568         Pine      204   3    3
# 13 202568       Spruce      132   1    1
# 14 202568       Spruce       93   2    2