我正在尝试确定与零不同的第一个小数位值。例如,在0.0000082109314
中,它将是第六个(或百万个),其中有8
。
我想过一个循环,但它没有用。所以我宁愿在 pseudo -code:
中提问d = runif(100, min = 1e-10, max = 1e-5) # Toy data with 100 elements in a vector
position = rep(0, 100) # Starting an empty vector to place results
for(j in 1:100){ # Looping through d
for(i in 1:10){ # Exponents from 1 to 10
if(d[j]]) * 10^i >= 1) # First power of 10 turning the value > or = 1
position[j] = i # Assign i to the position
stop the looping through i and move on to the next j
}
}
所以我需要循环在条件满足后立即停止替换i
值。否则,i
的任何更高值也将满足条件,并且它将不会返回与零不同的所需第一个位置。
我知道break
和next
,但我怎么能在这里使用它们(或其他命令)?
问题是在循环中的某个时刻(满足条件时)要求R为1.保存索引,然后2.转到下一个j
。
position = rep(0, 100)
for(j in 1:100){
for(i in 1:10){
if(d[j]]) * 10^i >= 0) position[j] = i AND next
else
CONTINUE with i
}
}
答案 0 :(得分:3)
if
会让你退出当前的循环。它不会一直到顶级,所以如果你在i索引的循环中使用它,它基本上只会踢你到j的下一个值,并在1重启i。
如果您要在for(j in 1:3){
for(i in 1L:6L){
# The result of this if statement is that we skip this iteration
# when i==2.
if(i == 2){
next
}
# The result of this if statement is that we kick out of the
# for loop indexed by i. The result being that we reach the end
# of the code block for the for loop indexed by j so if we aren't
# finished iterating over all of the values for j we go to the next
# value for j and start the for loop with i all over again.
if(i == 5){
break
}
# Just print out what i and j are equal to. We do this after the
# if statements so any iteration that isn't stopped by the if
# statements will end up printing a result.
print(sprintf("i: %i j: %i", i, j))
}
}
语句中包含多个条件,请务必将其全部用大括号括起来。
[1] "i: 1 j: 1"
[1] "i: 3 j: 1"
[1] "i: 4 j: 1"
[1] "i: 1 j: 2"
[1] "i: 3 j: 2"
[1] "i: 4 j: 2"
[1] "i: 1 j: 3"
[1] "i: 3 j: 3"
[1] "i: 4 j: 3"
给出输出
d = runif(100, min = 1e-10, max = 1e-5) # Toy data with 100 elements in a vector
position = rep(0, 100) # Starting an empty vector to place results
for(j in 1:100){ # Looping through d
for(i in 1:10){ # Exponents from 1 to 10
if((d[j] * 10^i) >= 1){ # First power of 10 turning the value > or = 1
position[j] = i # Assign i to the position
break
}
}
}
所以通过使用next,我跳过每次迭代,其中i == 2并且通过使用break我停止任何i> = 5并继续j的下一个值
如果您在使用代码时遇到麻烦,则需要发布您实际尝试过的内容。在您的代码中存在除“break”之外的其他问题(您使用d [j]]注意不匹配的方括号,我认为您在if语句中弄乱了括号)。这就是我认为你想要的:
@Entity
@Table(name = "folder")
public class Folder {
@Id
@SequenceGenerator(name = "folder_seq_gen", sequenceName = "FOLDER_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "folder_seq_gen")
private long id;
@Column
private Date date;
@OneToMany(mappedBy = "folder_documents_compositeKey.folder",
cascade = CascadeType.ALL)
private Set<Folder_Documents> folder_documents;
答案 1 :(得分:1)
避免循环的方法是:
对于您的示例,它看起来像这样(var1
=您的输入数据,place
是结果小数位):
var1<-0.0000082109314
var1<-as.character(format(var1, scientific = T))
place<-strsplit(var1,"e")
place<-abs(as.numeric(place[[1]][2]))
(假设你有值&lt;&lt; 1.当然,大值的输入数据必须以不同的方式解释。)
答案 2 :(得分:1)
如果你想使用循环,我认为这类问题是while
循环的理想选择,因为你事先并不知道需要多少次迭代。 (由于一个小错字,它们也更有可能开始永远运行,要求你重新启动你的R会话。)
x = 0.0000082109314
zero_first = TRUE
exponent = -1
while(zero_first) {
exponent = exponent + 1
if(x * 10^exponent >= 1) zero_first = FALSE
}
exponent
# [1] 6
这可以替换你的内部for
循环。当然,它有风险,所以最好做一些输入检查。