Poll of the Day > Has anyone here ever worked with R?

Topic List
Page List: 1
MrMelodramatic
09/12/18 11:03:10 PM
#1:


I need to get a random sample of 20 numbers between 1 and 50, find the mean and save it as a variable, and then repeat that process 1000 times.

I know how to do it by brute force
x1 <- Sample(1:50, 20)
m1 <- mean(x1)


But does anyone here know how I can do it without literally typing everything out a thousand times
---
Proud to be EPic
Texas Aggie, Class of 2018 A-Whoop!
... Copied to Clipboard!
EclairReturns
09/12/18 11:04:30 PM
#2:


I worked with R exactly one year ago. Then I decided that it was much easier to do my probability/statistics homework on an Excel sheet.

So I can't really answer your question, because I remember nothing of what I learned of the program.
---
Number XII: Larxene.
The Organization's Savage Nymph.
... Copied to Clipboard!
MrMelodramatic
09/12/18 11:05:51 PM
#3:


I have to show my work in R :(

I dont want to spend a billion hours typing all of this. There must be an easier way D:
---
Proud to be EPic
Texas Aggie, Class of 2018 A-Whoop!
... Copied to Clipboard!
EclairReturns
09/12/18 11:09:42 PM
#4:


MrMelodramatic posted...
There must be an easier way


That sounds like something from an infomercial. In any case, I know that I'm not helping, so sorry.
---
Number XII: Larxene.
The Organization's Savage Nymph.
... Copied to Clipboard!
MrMelodramatic
09/12/18 11:13:55 PM
#5:


I put my laptop away for the night but maybe I can make the code into one line... something like

m1 <- mean(sample(1:50, 20))

I dont know if it works that way, and also that doesnt solve my thousand times problem. Blah.
---
Proud to be EPic
Texas Aggie, Class of 2018 A-Whoop!
... Copied to Clipboard!
Kungfu Kenobi
09/12/18 11:27:47 PM
#6:


Wait, they didn't teach you loops before giving you this assignment?
---
This album is not available to the public.
Even if it were, you wouldn't wanna listen to it!
... Copied to Clipboard!
MrMelodramatic
09/12/18 11:30:05 PM
#7:


No
---
Proud to be EPic
Texas Aggie, Class of 2018 A-Whoop!
... Copied to Clipboard!
Chewster
09/12/18 11:36:40 PM
#8:


I don't know anything about R but this sounds like something where you'd want to use a for loop, and I googled "r for loop" and it appears to be a thing. So just google it and if you can't figure it out from there I don't know what to tell you, programming probably isn't your thing
... Copied to Clipboard!
trogita
09/12/18 11:42:02 PM
#9:


Yeah, you should just close the door to an entire career path because you're having trouble with this one homework problem /sarcasm
... Copied to Clipboard!
funkyfritter
09/12/18 11:44:59 PM
#10:


-write it out once and copy the line
-paste 9 times so you have 10 total, then copy that block
-repeat twice
---
And with that...pow! I'm gone!
... Copied to Clipboard!
MrMelodramatic
09/12/18 11:46:15 PM
#11:


Programming is most definitely not my thing, but that doesnt stop this class from being required! Ill look into the loop thing tomorrow. Thanks for the tip.
---
Proud to be EPic
Texas Aggie, Class of 2018 A-Whoop!
... Copied to Clipboard!
MrMelodramatic
09/12/18 11:46:47 PM
#12:


funkyfritter posted...
-write it out once and copy the line
-paste 9 times so you have 10 total, then copy that block
-repeat twice

But Id have to go in and manually change the variable names
---
Proud to be EPic
Texas Aggie, Class of 2018 A-Whoop!
... Copied to Clipboard!
Magus 10
09/13/18 12:21:55 AM
#13:


Looks like R supports for loops just fine:

https://www.datacamp.com/community/tutorials/tutorial-on-loops-in-r

# Create a vector filled with random normal values
u1 <- rnorm(30)
print("This loop calculates the square of the first 10 elements of vector u1")

# Initialize `usq`
usq <- 0

for(i in 1:10) {
# i-th element of `u1` squared into `i`-th position of `usq`
usq[i] <- u1[i]*u1[i]
print(usq[i])
}

print(i)


It should be simple enough to modify that example to what you need.
---
Internet = Tube0 + Tube1X + Tube2X^2/2! + Tube3X^3/3! + Tube4X^4/4! + ...
... Copied to Clipboard!
jramirez23
09/13/18 12:35:11 AM
#14:


Quick way:

j = rep(NA, 1000)
for (i in 1:1000) {
j[i] = 10
}


j is a vector with 1000 entries, and it starts with all of them being garbage (NA). Then inside the for loop it populates all entries of j with the number 10.

You would want to change the line inside the loop to something like j[i] = mean(sample(1:50, 20))

Then, if you wanted to output the mean of the 135th sample you took, you just type j[135] to see what number that entry has.
---
I consider it completely unimportant who in the party will vote, or how; but what is extraordinarily important is this who will count the votes, and how.
... Copied to Clipboard!
MrMelodramatic
09/13/18 11:12:45 AM
#15:


Okay, so

for (i in 1:1000) {
m[i] = mean(sample(1:50, 20))
}


looks like it's working. I don't know what

jramirez23 posted...
j = rep(NA, 1000)

this line is supposed to do for me
---
Proud to be EPic
Texas Aggie, Class of 2018 A-Whoop!
... Copied to Clipboard!
Krazy_Kirby
09/13/18 11:35:58 AM
#16:


thought this was going to be about the "r" word
---
... Copied to Clipboard!
SmokeMassTree
09/13/18 3:44:57 PM
#17:


I've worked with many Rs before
---
A.K. 2/14/10 T.C.P.
Victorious Champion of the 1st Annual POTd Hunger Games and the POTd Battle Royale Season 3
... Copied to Clipboard!
Andromicus
09/13/18 6:38:11 PM
#18:


Just put the numbers in a mathulator ya dingus
---
PotD's official master braider
... Copied to Clipboard!
Topic List
Page List: 1