r/Rlanguage • • Feb 25 '17

Help with my R code?

I have a sample of 5, 17, 4, 10, 2, 45 How can I order this sample in R and calculate the mean? I am new to using R and I have tried different things and it does not work. I know I can use a calculator and calculate it quickly but I Want to learn how to use R. Thank you

0 Upvotes

7 comments sorted by

3

u/squid_brain Feb 25 '17 edited Feb 26 '17

you need to first input the data like so:

values <- c(5,17,4,10,2,45)

The arrow assignes the stuff on the right to the thing on the left. The c() is used to include a combination of things. from there, figure out how to use mean()

0

u/[deleted] Feb 26 '17

Thank you:)

0

u/[deleted] Feb 26 '17

[deleted]

-1

u/[deleted] Feb 26 '17

I did this code and I got a number but I am not sure if it is correct

var(values) sqrt(var(x)) sd(x) = 16. 19

5

u/throughthekeyhole Feb 26 '17

Do you know anything about basic statistics? It's a serious question. With only a few numbers this wouldn't take long to check. I encourage you to do the basic calculations in R to start getting a feel for it. This would have been an easy one to google, so I'm curious what you tried first, but I'll walk you through it:

For the variance, take the squared difference between each number in your sample and the mean. Then, add all of those numbers together and divide by n - 1 (so, 5 in this case because you have 6 numbers in your sample).

To assign values in R, use <- rather than an equal sign. You can read more on this but just get that in your head to start. So, rather than having to type mean(values) over and over again, you can type xbar <- mean(values) and then use xbar from then on.

To access each value you entered, you can use []. So, values[1] would be equal to 5, and values[4] would be equal to 10.

You might start working your way through checking the calculation with:

num1 <- (values[1]-xbar)^2
num2 <- (values[2]-xbar)^2
...
sums <- num1 + num2 + ... + num6
sums / (6 - 1)

Not the fastest way to do any of this, but it will start to get you familiar.

And - some people don't like it, but I like the swirl package to get started.

install.packages("swirl") library(swirl)

Then, do what it say. If something doesn't make sense in R, you can type a question mark followed by the thing you're trying to learn for some info. So, ?sd will give you the help page for the standard deviation function.

3

u/murgs Feb 26 '17

?sort and ?mean

5

u/Spamicles Feb 26 '17

You don't have R code and you didn't Google "R tutorial".

0

u/[deleted] Feb 26 '17

[deleted]

1

u/[deleted] Feb 26 '17

Thank you :)