从下一个中断点R的迭代继续循环

时间:2018-05-05 08:26:04

标签: r loops

可能有一个简单的解决方案,但我正在努力。

我的代码如下:

for(i in 1:nrow(df)){
x[i] <- df[i,]$X4
if(length(unique(df[1:i,]$X4)) == length(unique(df$X4))){
break
}
collect <- data.frame(df[1,]$X1, df[1,]$X2, df[i+1,]$X3)
}

在达到 if 条件length(unique(df[1:i,]$X4)) == length(unique(df$X4))后,循环中断。但是,我希望从i+1'迭代开始再次启动相同的循环,并继续检查,直到再次满足相同的 if 条件,直到我的数据帧结束。

我的样本数据如下:

1       930000  1000000 E2-A
1       1890000 2110000 E2-A
1       2120000 2330000 D
1       2340000 3350000 E2-B
1       3365000 3405000 B
1       5695000 5810000 E2-A
1       6305000 6405000 E2-B
1       6425000 6465000 E1-A
1       6780000 6960000 E2-B
1       7100000 7270000 D
1       7730000 7810000 D 
1       8030000 8380000 E2-A
1       8970000 9170000 E1-A
1       9345000 9555000 E1-B
1       9845000 9930000 E1-A
1       10000000        10100000        E1-B
1       10430000        10560000        E3
1       11720000        11780000        B
1       11900000        11960000        C
1       12185000        12270000        E1-A
1       12450000        12680000        A  #break point of loop because if(length(unique(df[1:i,]$X4)) == length(unique(df$X4)))
1       13990000        14290000        B
1       15250000        15355000        E2-B
1       15475000        15600000        D
1       15655000        15755000        A
1       15920000        16080000        E2-A
1       16120000        16280000        C
1       16400000        16570000        E1-B
1       17280000        17380000        E1-B
1       17450000        17735000        A
1       17760000        17820000        E1-B
1       17825000        17935000        A
1       18925000        19150000        E1-A
1       19220000        19410000        C
1       19680000        19980000        C
1       20230000        20820000        E3 #the if condition is met again after the break, but using break exits the loop
1       20845000        20970000        E2-A
1       21580000        21695000        D
1       21700000        21920000        E2-A
1       22430000        22750000        B
1       22740000        22980000        A
1       23300000        23515000        C
1       23870000        23965000        A
1       24525000        24720000        E2-B
1       25010000        25160000        D
1       25170000        25430000        B
1       25930000        26130000        A
1       26220000        26330000        E2-B
1       26435000        26485000        C

我的预期输出是:

1       930000        12680000        
1       13990000      20820000

但到目前为止我得到的是:

1       930000        12680000        

我该怎么做?

2 个答案:

答案 0 :(得分:0)

# I saved the data you provided into a file and read them back into R session
df <- read.table("df.txt",quote="#")

# It looks like you are using X1, X2, and so on in your code example
# so I renamed the column names
names(df) <- c("X1","X2","X3","X4")

# check the structure of the data frame
    str(df)
# 'data.frame': 49 obs. of  4 variables:
#   $ X1: int  1 1 1 1 1 1 1 1 1 1 ...
# $ X2: int  930000 1890000 2120000 2340000 3365000 5695000 6305000 6425000 6780000 7100000 ...
# $ X3: int  1000000 2110000 2330000 3350000 3405000 5810000 6405000 6465000 6960000 7270000 ...
# $ X4: Factor w/ 9 levels "A","B","C","D",..: 7 7 4 8 2 7 8 5 8 4 ...

result <- list()
i.new = 1
j = 0
# number of unique values in the 4th column
n.unique <- length(unique(df$X4))
for ( i in seq(nrow(df) )) {

  if(length(unique(df[i.new : i,"X4" ])) == n.unique ){
    j = j+1
    result[[j]] <- c( df[i.new, 2], df[i, 3])
    i.new = i + 1
  }
}

result
# [[1]]
# [1]   930000 12680000
# 
# [[2]]
# [1] 13990000 20820000



# If Dataframe is needed:
do.call(rbind.data.frame, result)
#c.930000L..13990000L. c.12680000L..20820000L.
#1                930000                12680000
#2              13990000                20820000

#If matrix is OK
matrix(unlist(result, use.names=F), ncol = 2, byrow = TRUE)
#         [,1]     [,2]
#[1,]   930000 12680000
#[2,] 13990000 20820000

答案 1 :(得分:0)

compile 'com.google.android.gms:play-services:12.0.1'