Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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_Loops_Numbers_Sequence_Cumsum - Fatal编程技术网

使用累积和获取自定义数字序列的R代码是什么?

使用累积和获取自定义数字序列的R代码是什么?,r,loops,numbers,sequence,cumsum,R,Loops,Numbers,Sequence,Cumsum,我的输入列在数据框的左侧。我需要我的输出列看起来像第二列。左侧的任何连续序列都必须以右列序列开始的输出为结果。谢谢您可以通过diffInput是否大于1来定义组 资料 这是一个带有data.table的解决方案 到目前为止你尝试了什么?你尝试了什么?你打算用什么语言?请考虑阅读。 Input Output 1001 1001 1067 1067 1068 1067 1080 1080 1081 1080 1082 1080 1255 1255 125

我的输入列在数据框的左侧。我需要我的输出列看起来像第二列。左侧的任何连续序列都必须以右列序列开始的输出为结果。谢谢

您可以通过diffInput是否大于1来定义组

资料

这是一个带有data.table的解决方案


到目前为止你尝试了什么?你尝试了什么?你打算用什么语言?请考虑阅读。
Input   Output
1001    1001
1067    1067
1068    1067
1080    1080
1081    1080
1082    1080
1255    1255
1256    1255
1257    1255
1258    1255
1259    1255
1386    1386
1822    1822
library(dplyr)
df %>%
  group_by(G = cumsum(c(0, diff(Input)) > 1)) %>%
  mutate(Output = min(Input)) %>%
  ungroup() %>%
  select(-G)

# A tibble: 13 x 2
   # Input Output
   # <int>  <dbl>
 # 1  1001   1001
 # 2  1067   1067
 # 3  1068   1067
 # 4  1080   1080
 # 5  1081   1080
 # 6  1082   1080
 # 7  1255   1255
 # 8  1256   1255
 # 9  1257   1255
# 10  1258   1255
# 11  1259   1255
# 12  1386   1386
# 13  1822   1822
df <- read.table(text="Input   Output
1001    1001
1067    1067
1068    1067
1080    1080
1081    1080
1082    1080
1255    1255
1256    1255
1257    1255
1258    1255
1259    1255
1386    1386
1822    1822", header=TRUE)
library("data.table")
DT <- fread(
"Input  
1001    
1067    
1068    
1080    
1081    
1082    
1255    
1256    
1257    
1258    
1259    
1386    
1822")
DT[, Output:=min(Input), cumsum((c(0, diff(Input))>1))]
DT