Vectorized IF statement in R

0 votes
y - if (x 5) seq(0.1,10,0.1) x - seq(0.1,10,0.1) 1 if not 2

The condition length > 1 is shown by a warning (or an error from R version 4.2.0).

Instead of operating on the entire vector, I'd like the if to operate on each individual case. What do I need to alter?
Jun 17, 2022 in Data Analytics by Avinash
• 1,260 points
1,592 views

1 answer to this question.

0 votes
x <- seq(0.1,10,0.1)

> x
  [1]  0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1.0  1.1  1.2  1.3  1.4  1.5
 [16]  1.6  1.7  1.8  1.9  2.0  2.1  2.2  2.3  2.4  2.5  2.6  2.7  2.8  2.9  3.0
 [31]  3.1  3.2  3.3  3.4  3.5  3.6  3.7  3.8  3.9  4.0  4.1  4.2  4.3  4.4  4.5
 [46]  4.6  4.7  4.8  4.9  5.0  5.1  5.2  5.3  5.4  5.5  5.6  5.7  5.8  5.9  6.0
 [61]  6.1  6.2  6.3  6.4  6.5  6.6  6.7  6.8  6.9  7.0  7.1  7.2  7.3  7.4  7.5
 [76]  7.6  7.7  7.8  7.9  8.0  8.1  8.2  8.3  8.4  8.5  8.6  8.7  8.8  8.9  9.0
 [91]  9.1  9.2  9.3  9.4  9.5  9.6  9.7  9.8  9.9 10.0

> ifelse(x < 5, 1, 2)
  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 [38] 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

Enhance your data skills with our comprehensive Data Analyst Certification – Enroll now!

answered Jun 24, 2022 by Sohail
• 3,040 points

Related Questions In Data Analytics

0 votes
1 answer

Check if the object(variable) is defined in R

You can use the exists(): > exists("toFindUnknown") [1] FALSE > ...READ MORE

answered Apr 17, 2018 in Data Analytics by Sahiti
• 6,370 points
807 views
+1 vote
2 answers

How to replace a value in a data frame based on a conditional 'If' statement?

It's easier to convert alpha to characters ...READ MORE

answered Jun 6, 2018 in Data Analytics by Sahiti
• 6,370 points
36,678 views
0 votes
1 answer

"Error in if" while trying to execute simple code in R

This caused non-logical data or missing values passed ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 52,350 points
632 views
0 votes
1 answer

If-else in R code

x <- c(.5, 0.2, 2, 3.4, 0.4, ...READ MORE

answered Nov 5, 2018 in Data Analytics by Kalgi
• 52,350 points
763 views
0 votes
1 answer

How to create excellent examples in R?

An excellent example must consist of the ...READ MORE

answered Apr 10, 2018 in Data Analytics by kappa3010
• 2,090 points

edited Apr 12, 2018 by kappa3010 773 views
0 votes
1 answer

Add regression line equation and R^2 on graph

Let us look at one of the ...READ MORE

answered Mar 23, 2022 in Machine Learning by Dev
• 6,000 points
3,666 views
0 votes
1 answer

Test if a vector contains a given element

1. Making use of the std::count function Counting ...READ MORE

answered Jun 20, 2022 in Data Science by Sohail
• 3,040 points
516 views
0 votes
1 answer

do-while loop in R

Unlike for and while loops, which test the loop condition at ...READ MORE

answered Jun 1, 2022 in Data Science by Sohail
• 3,040 points
681 views
0 votes
1 answer

How to use the switch statement in R functions?

Switch definitely wasn't intended to operate this ...READ MORE

answered Jun 24, 2022 in Data Analytics by Sohail
• 3,040 points
607 views
0 votes
1 answer

Use of $ and %% operators in R

According to help('percent in percent'), percent in ...READ MORE

answered Jun 23, 2022 in Data Analytics by Sohail
• 3,040 points
771 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP