x=runif(10,1,15) # random x coord
y=runif(10,1,15) # random y coord
a=runif(10,1,15)
b=runif(10,1,15)
x=as.integer(x)
y=as.integer(y)
a=as.integer(a)
b=as.integer(b)
onlineloc=cbind(x,y)
instore=cbind(a,b)
if(instore==onlineloc)
print(1)
else
print=0
I assume that you want the conditional statement to return TRUE
if the instore
matrix is identical to the onlineloc
matrix and FALSE
otherwise? There are a couple of ways to accomplish this, using all
or identical
.
all(instore == onlineloc)
[1] FALSE
or
identical(instore, onlineloc)
[1] FALSE
Using identical
is a bit cleaner.
x=runif(10,1,15) # random x coord
y=runif(10,1,15) # random y coord
a=runif(10,1,15)
b=runif(10,1,15)
x=as.integer(x)
y=as.integer(y)
a=as.integer(a)
b=as.integer(b)
onlineloc=cbind(x,y)
instore=cbind(a,b)
if (identical(instore, onlineloc)) print(1) else print(0)
[1] 0
all and identical will return false if any single value donot match what i want is following
- it should match both coordinates ie x and y of each location of one matrix with other(instore and online)
- either give only matched locations or give status of all locations
x y
7 13 loc 1
4 7 loc 2
7 13 loc 3
2 4 loc 4
8 5 loc 5
10 7 loc 6
2 5 loc 7
8 11 loc 8
5 12 loc 9
11 1 loc 10
> instore
x y
10 5 loc 1
10 14 loc 1
11 8 loc 3
14 11 loc 4
4 13 loc 5
3 14 loc 6
9 11 loc 7
11 5 loc 8
12 12 loc 9
4 13 loc 10
Take a look at this code
# Simple f for creating example data
f = function(){
return(sample(x = seq(from = 1, to = 15), size = 10))
}
# Set X data matrix
X = data.frame(x1 = f(), x2 = f())
# Set Y data matrix
Y = data.frame(y1 = f(), y2 = f())
# Which elements are equal?
X == Y
# Are any elements equal?
any(X == Y)
# Are all elements equal?
all(X == Y)
Hope it helps
x=1:5
y=11:15
a=1:5
b=15:19
x=as.integer(x)
y=as.integer(y)
a=as.integer(a)
b=as.integer(b)
onlineloc=cbind(x,y)
instore=cbind(a,b)
match(instore,onlineloc)
it is treating x and y seperately not as one kindly suggest the right way
Are you looking for rows where (x,y) == (a,b)? You can do that like this:
x=1:5
y=11:15
a=1:5
b=15:19
onlineloc=cbind(x,y)
instore=cbind(a,b)
onlineloc
#> x y
#> [1,] 1 11
#> [2,] 2 12
#> [3,] 3 13
#> [4,] 4 14
#> [5,] 5 15
instore
#> a b
#> [1,] 1 15
#> [2,] 2 16
#> [3,] 3 17
#> [4,] 4 18
#> [5,] 5 19
(onlineloc[,1] == instore[,1]) & (onlineloc[,2] == instore[,2])
#> [1] FALSE FALSE FALSE FALSE FALSE
Created on 2019-08-18 by the reprex package (v0.2.1)
f = function(){
return((a = runif(10,1,10))
a=as.integer(a)
}
X = data.frame(x = f(), y = f())
Y = data.frame(a = f(), b = f())
if(any(X == Y)){
print(X)
} else{print("0")}
how i can get that value only which is equal instead of all values and why it is generating 15 values instead of 10
Your function f has a mistake in it. Note how my version below is different.
Your if() checks if any element of the matrix returned by X==Y is TRUE and returns the entire data frame X if so.
Here are two ways to get the rows where X and Y are equal.
f <- function(){
a = runif(10,1,5)
return(as.integer(a))
}
set.seed(43634)
X = data.frame(x = f(), y = f())
Y = data.frame(a = f(), b = f())
#rows 1, 2 and 6 are euqal in X and Y
X
#> x y
#> 1 2 3
#> 2 2 2
#> 3 3 3
#> 4 4 3
#> 5 1 3
#> 6 1 4
#> 7 3 4
#> 8 2 4
#> 9 3 2
#> 10 1 3
Y
#> a b
#> 1 2 3
#> 2 2 2
#> 3 4 4
#> 4 2 2
#> 5 3 3
#> 6 1 4
#> 7 4 4
#> 8 3 4
#> 9 1 1
#> 10 1 2
Z <- X == Y
X[Z[,1] & Z[,2], ]
#> x y
#> 1 2 3
#> 2 2 2
#> 6 1 4
#or use apply()
X[apply(X == Y, 1, all), ]
#> x y
#> 1 2 3
#> 2 2 2
#> 6 1 4
Created on 2019-08-31 by the reprex package (v0.2.1)
Thanks alot what if i want to run if statement for particular time ie it should not come out till the time if condition breaks the code is not working kindly guide
eg
deltime=readline(prompt="Please enter deliery date and time\t") # for input of date and time
T=as.POSIXlt.character(deltime) # to pass the time 2019-9-3 12:45:00 PKT
if(Sys.time()<T){
f = function(){
a = runif(100,1,10)
return( a=as.integer(a))
}
onlineloc = data.frame(x = f(), y = f())
instore = data.frame(a = f(), b = f())
Z<- onlineloc == instore
onlineloc[Z[,1] & Z[,2], ]
}
else{print("Enter Valid time")}
This seems to work. I just moved the else on to the same line as the preceding } and I wrapped the line
onlineloc[Z[,1] & Z[,2], ]
in a print().
deltime=readline(prompt="Please enter deliery date and time\t") # for input of date and time
T=as.POSIXlt.character(deltime) # to pass the time 2019-9-3 12:45:00 PKT
if(Sys.time()<T){
f = function(){
a = runif(100,1,10)
return( a=as.integer(a))
}
onlineloc = data.frame(x = f(), y = f())
instore = data.frame(a = f(), b = f())
Z<- onlineloc == instore
print(onlineloc[Z[,1] & Z[,2], ])
} else{print("Enter Valid time")}
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.