how to write an R script that takes from the user a list of numbers and do some mathematical operations on it ?
0
0
Entering edit mode
7 weeks ago
m90 ▴ 30

i want to know how to write R script that takes from the user a list of numbers and outputs the following of the list:

  1. Mean and median.
  2. Standard deviation and range.

i want it to receive any input data from the keyboard and display data on the screen.

here the script that i used :

num <- readline (prompt = "list of numbers")

num <- as.numeric(num)

result.mean <- mean(num)

print (result.mean)

result.median <- median(num)

print(result.median)

result.sd <- sd(num)

print(result.sd) 

but it didnt work and i'm sorry if the script not in correct format but i dont know how to display it .

script R • 356 views
ADD COMMENT
0
Entering edit mode

Please use the formatting bar (especially the code option) to present your post better. I've done it for you this time.
code_formatting

ADD REPLY
0
Entering edit mode

thanks alot

ADD REPLY
0
Entering edit mode

It only works for a single number input, if you wish to accept multiple numbers separated with comma or space, we need so split it on delimiter before converting to numeric. See related StackOverflow post:

ADD REPLY

Login before adding your answer.

Traffic: 2247 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6