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.
Related
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"}
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")
}
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
The function follows:
qual <- function(x, y, z)
{if ((abs(x - y) <= .15)&(abs(x - z) <= .15)&(abs(y - z) <= .15)){print("grade A")}
else if((abs(x - y) <= .15)|(abs(x - z) <= .15)){print("grade B")}
else if((abs(x - y) <= .2)|(abs(x - z) <= .2)){print("grade C")}
else if((abs(x - y) <= .25)|(abs(x - z) <= .25)){print("grade D")}
else {print("check manually")}}
It seems that, e.g., the output of qual(1.19, 1.04, 1.06) and qual(1.10, .95, .97)should be "grade A". However, the output is "grade A" and "grade B", respectively.
Why is this?
I think you are hitting some floating point precision problems. See Why are these numbers not equal for a full explanation.
To fix this, you can use a short function that takes into account minor precision errors when comparing:
less_equal_safe <- function(x, y) {
(x < y) | dplyr::near(x, y)
}
qual <- function(x, y, z) {
if (less_equal_safe(abs(x - y), .15) & (abs(x - z) <= .15) &(abs(y - z) <= .15)) {
print("grade A")
} else if ((abs(x - y) <= .15) | (abs(x - z) <= .15)) {
print("grade B")
} else if ((abs(x - y) <= .2) | (abs(x - z) <= .2)) {
print("grade C")
}
else if ((abs(x - y) <= .25) | (abs(x - z) <= .25)) {
print("grade D")
} else {
print("check manually")
}
}
(note that you need the dplyr package to be installed to use dplyr::near)
I've only replaced the first comparison, which is the one that was causing the issue, but ideally you should replace all comparisons in your function with the float-safe function.
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 8 years ago.
Improve this question
I need to have a user enter a number from 0 to 1 (nth number). Returning the nth largest number within say a vector. So when n=1 it acts as the maximum of the vector outputting the largest number. I am new to coding in r. Any help on improving my code would be grateful let alone getting it to work.
KFUN <- function() {
bob <- c(1,2,3,6)
ANSWER <- readline("Enter k value:")
k <- ANSWER
if(k <= 1 && k >= 0) {
if(0 < k <= .25) {
bob[c(4)]
}
if(.25 < k && k <= .5) {
bob[c(3)]
}
if(.5 < k && k <= .75) {
bob[c(2)]
}
if(.75 < k && k <=1) {
bob[c(1)]
}
}
else {
stop("That is not within 0 to 1!")
}
}
KFUN()
Best
Try 'order' function:
nth_largest= function(vect, n){
vect[rev(order(vect))][n]
}
bob <- c(3,6,1,2)
nth_largest(bob, 1)
[1] 6
nth_largest(bob, 2)
[1] 3
nth_largest(bob, 3)
[1] 2
nth_largest(bob, 4)
[1] 1
Hard to explain the problem in the title, so please read on.
I have a project where we are implementing a sobel filter. At first, the image didn't work as it should do, with the sobel calculations
Gx <= ("000" & p3-p1)+(("00" & p6 & '0')-("00" & p4 & '0'))+("000" & p9-p7);
Gy <= ("000" & p7-p1)+(("00" & p8 & '0')-("00" & p2 & '0'))+("000" & p9-p3);
but with the same calulation expressed in a different way
Gx <= ("000" & p3)+("00" & p6 & '0')+("000" & p9)-("000" & p1)-("00" & p4 & '0')-("000"
& p7);
Gy <= ("000" & p7)+("00" & p8 & '0')+("000" & p9)-("000" & p1)-("00" & p2 & '0')-("000"
& p3);
it worked perfectly. Still, the simulations of the filter alone is exactly the same.
Has it something to do with how I pad the zeroes before the vectors?
Without knowing the data types I'm not completely sure if this is the cause, but my guess would be that if p1 > p3 (and some of the other ones too) you'll get different results from the two types of calculations. Pseudo-code example:
p1 = "010";
p3 = "001";
--Method1:
res = "000" & p3-p1 = "000" & "111" = "000111";
--Method2
res = ("000" & p3) - ("000" & p1) = "000001" - "000010" = "111111";