If else sequences Exercises

November 22, 2016
By

(This article was first published on R-exercises, and kindly contributed to R-bloggers)

index

Answers to the exercises are available here.

Exercise 1

What is the output of:
ifelse(sqrt(9)<2,sqrt(9),0)

Exercise 2

What is the output of:
ifelse(sqrt(100)>9,sqrt(100),0)

Exercise 3
What is the output y of:
x=12
if(is.numeric(x)) y=x*2

Exercise 4

What is the output x,y of:

z=-1
if(z<0){x=abs(z);y=z*3}

Exercise 5

What is the output y of:
z=6
if(z<0) y=z*3 else y=z*5

Exercise 6

What is the output z of:
x=15
y=3
if(is.numeric(x))
if(is.numeric(y) & y!=0)
z=x/y

Exercise 7

What is the output of:
x=letters[20]
if (is.numeric(x)) print('is numeric') else
if(is.character(x)) print('is character')

Exercise 8

What is the output of:

x=90
ifelse(x<100,ifelse(x/2==trunc(x/2),x/2,0),
ifelse(x/100==trunc(x/100),x/100,-1))

x=120
ifelse(x<100,ifelse(x/2==trunc(x/2),x/2,0),
ifelse(x/100==trunc(x/100),x/100,-1))

x=200
ifelse(x<100,ifelse(x/2==trunc(x/2),x/2,0),
ifelse(x/100==trunc(x/100),x/100,-1))

Exercise 9

What is the output n of:

z='i'
if (z %in% letters)
if (z=='a') n=1 else
if (z=='e') n=2 else
if (z=='i') n=3 else
if (z=='o') n=4 else n=5

Exercise 10

What is the output n of:

z='u'
if (z %in% letters)
if (z=='a') n=1 else
if (z=='e') n=2 else
if (z=='i') n=3 else
if (z=='o') n=4 else n=5

To leave a comment for the author, please follow the link and comment on their blog: R-exercises.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)