我正在尝试解决在hackerrank上发布的b=b+(-12); a++
中比较三元组的问题
虽然我已经概述了权利的步骤仍然没有给出正确的结果,而不是在hackerrank和R
。任何人都可以告诉我
可重现的例子
RStudio
我在其他每个人之后都会遇到以下错误
m = data.frame(ints = as.integer()) m <- structure(rbind(m,c(5,6,7)), .Names = names(m)) m <- structure(rbind(m,c(3,6,10)), .Names = names(m)) names(m) = c("no1","no2","no3") enter## the output gives m as below `no1 no2 no3 1 5 6 7 2 3 6 10 ## I need to compare the corresponding values in both rows #if m[1,1] != m[2,1] then I need to store 1 in a vector or dataframe #if m[1,2] != m[2,2] then I need to store 1 in a vector or dataframe #if m[1,3] != m[2,3] then I need to store 1 in a vector or dataframe #so We will get output as [1,1] ## defining a vector to store output as below g = c(0,0,0) g = c(0,0,0) > g [1] 0 0 0 > g[1] [1] 0 ## so my answer is as below if(m[1,1]== m[2,1])) { print("nothing") } else { (g[1] = 1) } if((m[1,2]==m[2,2])) { print("nothing") } else { (g[2] = 1) } if((m[1,3]==m[2,3])) { print("nothing") } else { (g[3] = 1) } g = data.frame() g = c(0,0,0)
同样Error: unexpected 'else' in " else"
甚至取值中间值,它永远不应该
g
任何人都可以解释为什么它仍然为中间值放置1。
答案 0 :(得分:1)
在R
中,行的末尾标记指令的结束,除非有开括号,开括号或未完成的指令,例如else
以表示否则。
尝试
if(m[1,1]== m[2,1]) {
print("nothing")
} else
{
(g[1] = 1)
}
或更短
if(m[1,1]== m[2,1])
print("nothing") else
g[1] = 1
在任何情况下,您的问题都可以通过以下方式解决:
g <- as.numeric(m[1,] != m[2,])
# [1] 1 0 1