I am trying to do an if & statment in R:
I want to do something like this:
if (x > 1) & (y = "Yes) {"replace")
I've also tried
if (x > 1) && (y = "Yes") {"replace")
Which I read on StackOverflow.
How do I convert the excel formula
=IF(AND(cell > 1, cell = "Yes"),100,0)
Try this. Does it work?
if (x > 1 & y == "Yes") {"replace"}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is there a way to create loop indices different from each other, without going through the combinations like I did here with the if statement?
for (i in names(Category[,-c(1:2, 13:63)])){
for (j in names(Category[,-c(1:2, 13:63)])){
for (z in names(Category[,-c(1:2, 13:63)])){
for (t in names(Category[,-c(1:2, 13:63)])){
for (k in names(Category[,-c(1:2, 13:63)])){
for (m in names(Category[,-c(1:2, 13:63)])){
for (l in names(Category[,-c(1:2, 13:63)])){
for (r in names(Category[,-c(1:2, 13:63)])){
for (s in names(Category[,-c(1:2, 13:63)])){
for (d in names(Category[,-c(1:2, 13:63)])){
if (z!=j & j!=i & z!=i & t!=z & t!=j & t!=i & k!=t & k!=z & k!=j & k!=i& m!=i & m!=z & m!=j & m!=t & m!=k & l!=i & l!=z & l!=j & l!=t & l!=k & l!=m & r!=i & r!=z & r!=j & r!=t & r!=k & r!=m & r!=l & s!=i & s!=z & s!=j & s!=t & s!=k & s!=m & s!=l & s!=r & d!=i & d!=z & d!=j & d!=t & d!=k & d!=m & d!=l & d!=r & d!=s ){
n<-n+1
assign(paste0("model_",n),lmer(
as.formula(
paste(
names(Category)[2], "~" ,paste(i, "+", j ,"+",z, "+",t, "+",k, "+",m, "+",l, "+",r, "+",s, "+",d),
'+ (1|', names(Category)[1], ')'
)
),
data=Category)) }}}}}}}}}}}
I suppose you are trying to generate a bunch of formulae by taking all possible combinations of variables. Take a look at expand.grid and apply. Here is a small example:
df <- data.frame("a"=letters[6:10],
"b"=letters[1:5],
"c"=letters[11:15])
df <- expand.grid(df)
apply(df[1:10,], MARGIN=1, FUN=function(x) paste("y ~",paste0(x,collapse = "+")))
Output
1 2 3 4 5 6
"y ~ f+a+k" "y ~ g+a+k" "y ~ h+a+k" "y ~ i+a+k" "y ~ j+a+k" "y ~ f+b+k"
7 8 9 10
"y ~ g+b+k" "y ~ h+b+k" "y ~ i+b+k" "y ~ j+b+k"
It is hard to provide a more targeted help without a reproducible example.
I have a problem with a school task -> BMI calculator
Here is my code:
#Przedziały
niedowaga <- seq(16.00, 18.40, 0.01)
norma <- seq(18.50, 24.90, 0.01)
nadwaga <- seq(25.00, 30.00, 0.01)
print(niedowaga)
print(norma)
print(nadwaga)
#Pytanie
waga = as.integer(readline(prompt="Podaj swoją wagę: "))
wzrost = as.integer(readline(prompt="Podaj swój wzrost w cm: "))
#Formuła
bmi <- waga/wzrost**2 * 10000
#Zaokrąglenie BMI do jednej liczby po przecinku
bmi_round <-round(bmi, digits = 2)
#Wyświetlenie wartości BMI po zaokrągleniu
print(bmi_round)
#Sprawdzenie BMI w oparciu o przedziały
for(bmi_round in niedowaga) {
if(bmi == niedowaga) {
print("Niedowaga")
}
}
else {
if (bmi == norma) {
print("Norma")
}
}
else if (bmi == nadwaga) {
print("Nadwaga")
}
I have three sequence variables, "niedowaga, norma, and nadwaga"
I calculated the BMI index.
Now I need to make a loop to check the computed BMI. "bmi_round" have to check to which sequence it fits - "niedowaga", "norma" and "nadwaga" (the first three variables) and give the output based on the computed BMI and sequence-
How can I do this?
Sorry for language in comments and in variables name - it's polish ;)
A loop is not needed for this:
waga = 30L #changed from readline
wzrost = 60L #changed from readline
bmi <- waga/wzrost**2 * 10000
ifelse(bmi >= 16 & bmi < 18.5, 'Niedowaga',
ifelse(bmi >= 18.5 & bmi < 25, 'Norma',
ifelse(bmi >=25 & bmi <= 30, 'nadwaga', 'outside normal range')))
# or
dplyr::case_when(bmi >= 16 & bmi < 18.5 ~ 'Niedowaga',
bmi >= 18.5 & bmi < 25 ~ 'Norma',
bmi >=25 & bmi <= 30 ~ 'nadwaga',
TRUE ~ 'outside normal range')
For your loop, there are overall errors. It appears that you are trying to compare the bmi_round variable with everything else. Instead, your loop isn't really doing anything - bmi_round is changing to each element of niedowaga in the loop and is not being used. Here is one way to change it
bmi_round <- 23
for(nied in niedowaga){
if (bmi_round == nied) print("Niedowaga")
}
for (norm in norma){
if (bmi_round == norm) print("Norma")
}
# [1] "Norma"
for (nad in nadwaga){
if (bmi_round == norm) print("Nadwaga")
}
Boolean algebra:
a + ~a.b
This is as far as I got:
= a + ~a.b + a.~a
= a + ~a(b + a)
I know that the answer is a + b however I can't seem to prove it.
I would be very grateful if someone could show the steps to proving it.
The way to solve this is to break it down.
I prefer to use & and | and !.
1. A & (!A | B)
2. (A & !A) | (A & B) Distributive property
3. 0 | (A & B) (A & !A) just like True and False can never be True
4. A & B
You may also want to look at De Morgan's Laws
One way to do this easily is:
= a + ~a.b
= a.(1+b) + ~a.b
= a + a.b + ~a.b
= a+ b.(a + ~a)
= a + b
Thanks!
I know the formula is: n(h) = n(h-1) + n(h-2) + 1
And I know it can be reduced as:
n(h) = n(h-1) + n(h-2) + 1
>= n(h-2) + n(h-2) + 1
>= 2n(h-2) + 1
>= 2n(h-2)
After this step I don't understand the recurrence that would come here. I was reading a proof online and they did this:
>= 2n(h-2)
>= 2(2n(h-4))
>= 2(2(2n(h-6)))
I'm not understanding that block. Why is each step multiplied by 2 and why is 2 more subtracted each time from the height? I'm having trouble visualizing it or something. Then the rest of the proof shows:
>=(2^i)n(h-2i)
I understand how they got that answer based on the pattern, and I can solve the rest of the proof, but I don't understand how that recursive pattern was chosen. I hope I'm making sense. If anyone can clarify this for me, I would appreciate it very much!
Given that n(h) >= 2n(h-2) for all h, we can apply this very same inequality for h-2 as well. This would give us:
n(h-2) >= 2n(h-2-2)
which is the same as
n(h-2) >= 2n(h-4).
If we now apply it again for h-4 (as we did for h-2) we get
n(h-4) >= 2n(h-4-2) = 2n(h-6).
Now we can replace these two last inequalities into the first one:
n(h) >= 2n(h-2) >= 2(2n(h-4)) >= 2(2(2n(h-6)))
and so on...