【问题标题】:Continuing a loop from the next iteration of point of break R从中断点 R 的下一次迭代继续循环
【发布时间】:2018-05-05 08:26:04
【问题描述】:

可能有一个简单的解决方案,但我正在苦苦挣扎。

我有一个代码如下:

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'th 迭代再次开始相同的循环,并继续检查直到再次满足相同的 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        

我该怎么做?

【问题讨论】:

  • 试试next而不是break
  • @Prem,不起作用。 next 会跳过我的if condition
  • 您可能希望向我们展示可重现的示例以及所需的输出。
  • 如果您提供了一个副本/可粘贴的自包含示例,它不仅可以帮助您,还可以帮助我们提供及时和高质量的帮助。有关许多示例,请参见 here
  • 请注意,我提到了复制/粘贴,这意味着我们将示例复制并粘贴到我们的 R 会话中,我们正在路上。您的示例涉及大量预处理,只是为了获取数据。有些人可能会被劝阻。

标签: r loops


【解决方案1】:
# 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

【讨论】:

  • &gt; do.call(rbind.data.frame, result) data frame with 0 columns and 0 rows :( 不起作用
  • 看起来你的初始变量有不同的结构。我在代码的开头添加了我是如何获得数据的。在我的数据和你的数据之间比较 str() 函数的输出
  • 我的第一列是字符,所以我把它们改成了整数,就像你的一样。我也将我的最后一列更改为因素,以匹配您的。然而错误仍然存​​在。我们应该把这个讨论带到聊天吗?
  • 当然,我们可以通过聊天进行工作。我现在必须做一些差事,但稍后会在聊天中回复您。
  • 我没有100声望哈哈,所以我不能创建聊天室。
【解决方案2】:
ulist<- unique(df$X4)
uniq <- length(unique(df$X4))
brk <- 0
rn <- length(df$X1)
coor <- NULL
while (brk < rn) {

found <- rep(0,uniq)
coor.s <- 0
coor.e <- 0

coor.s <- df$X2[brk+1]
for (i in (brk+1):rn) {
for (j in 1:uniq) {
  if(df$X4[i] == ulist[j]) {found[j]<-1}
}
if (sum(found)==uniq) {coor.e <- df$X3[i];brk=i;break}

}

if(sum(found)<uniq) {
break
} else {
collect.df <- as.data.frame(rbind(coor,c(coor.s,coor.e)))
}
}

【讨论】:

    猜你喜欢
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多