Reputation: 1
I am trying to create an "if... then
" statement in R
in which two variables, G1
and G2
, lie within a range of values in a given dataset. For example, the "if...then" scenario I would like to test is:
if (200 > G1 > -100) and (200 > G2 > -100), then G = mean(G1,G2)
My code for the scenario looks like this:
if(200 > G1 > -100 & 200 > G2 > -100) {G = mean(G1,G2)}
The error that I getting reads:
Error: unexpected '>' in " if(200 > G1 >"
Any help would be much appreciated! Thanks
UPDATE:
G1 and G2 are two lists of values in a data frame
Here is an example:
G1 = 101, G2 = 2
Expected output: G = mean(101,2) = 51.5
Upvotes: 0
Views: 87
Reputation: 9868
If you have your G1 and G2 in a dataframe, you can use dplyr::mutate()
rowMeans()
and replace()
values in rows not meeting the conditions with NA.
df%>%mutate(conditioned_mean=rowMeans(.)%>%
replace(., !if_all(G1:G2, ~.x>-100 & .x<200), NA))
G1 G2 conditioned_mean
1 125 -123 NA
2 135 122 128.5
3 142 180 161.0
4 215 140 NA
Data
G1 <- c(125, 135, 142, 215)
G2 <- c(-123, 122, 180, 140)
df<-data.frame(G1, G2)
Upvotes: 1
Reputation: 887511
The R
syntax is incorrect. It is different from the mathematical syntax
if(G1 > -100 & G1< 200 & G2 > -100 & G2 < 200) G <- mean(c(G1, G2))
-output
G
[1] 137.5
Having said that the if/else
works only when 'G1', 'G2' are of length
1 and not greater than 1 as if/else
is not vectorized i.e. if the length is more than 1, use the vectorized ifelse
or create a logical index, subset the values of 'G1', 'G2' based on that index and get the mean
G1 <- c(125, 135, 142, 215)
G2 <- c(-123, 122, 180, 140)
i1 <- G1 > -100 & G1< 200 & G2 > -100 & G2 < 200
mean(c(G1[i1], G2[i1]))
[1] 144.75
Or if we are interested in the mean
of corresponding elements that meet the condition, then cbind
the objects to a matrix
, subset the rows with 'i1' and get the rowMeans
rowMeans(cbind(G1, G2)[i1,])
[1] 128.5 161.0
G1 <- 150
G2 <- 125
Upvotes: 1