I am trying to solve the question of compare triplets in R posted on hackerrank Although I have outlined the entitre steps still its not giving correct result not in hackerrank and in R Studio also . can anyone tell me
A reproducible example
`
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)
I get the following errors after every else
Error: unexpected 'else' in " else"
also g even takes value for middle value which it should never take
g
[1] 1 1 1
Aucun commentaire:
Enregistrer un commentaire