The Student Room Group

R coding problem help needed.

Hello, I really need some help with this. Whatever I do I cannot get the R code to do what I want.

The problem is,

"In the box below, write some code that will construct a vector x of the integers from 1 to 100, and then replace the first 10 elements of x with missing values. Make sure that the last line of your code contains just x (by itself). This is for grading purposes".

First off I am a complete novice with using R as I am a Maths student and we need to learn how to use it for part of the course. I've been okay up until now but I can't get work out what to do.

I've tried

c((x=1:100),(x[1:10]=NA)) but I don't think this is correct.

Any help would be amazing. Also if you could show me some useful websites I could use to learn how to use R more effectively.
Reply 1
Anyone?? :smile:
Original post by Mr XcX
Hello, I really need some help with this. Whatever I do I cannot get the R code to do what I want.

The problem is,

"In the box below, write some code that will construct a vector x of the integers from 1 to 100, and then replace the first 10 elements of x with missing values. Make sure that the last line of your code contains just x (by itself). This is for grading purposes".

First off I am a complete novice with using R as I am a Maths student and we need to learn how to use it for part of the course. I've been okay up until now but I can't get work out what to do.

I've tried

c((x=1:100),(x[1:10]=NA)) but I don't think this is correct.

Any help would be amazing. Also if you could show me some useful websites I could use to learn how to use R more effectively.


'Missing values' sounds very ambiguous to me. But with your code and the x on the last line, putting that into R seems to give a reasonable output. I'm not sure what more they want.

For R, google is your friend - typing in questions will help. In R, you can also type in help(insert name of function here) and it'll bring up a page describing what the function does, what its inputs are etc.
Reply 3
Original post by SeanFM
'Missing values' sounds very ambiguous to me. But with your code and the x on the last line, putting that into R seems to give a reasonable output. I'm not sure what more they want.

For R, google is your friend - typing in questions will help. In R, you can also type in help(insert name of function here) and it'll bring up a page describing what the function does, what its inputs are etc.


Thank you for responding.

I'll try google tomorrow for more help on R I have a while before this is due.

So should I put my code as this,

c((x=1:100),(x[1:10]=NA)) x

with the x at the end. The wording is really confusing.
Reply 4
When I input the code I get,

c((x=1:100),(x[1:10]=NA))
[1] NA NA NA NA NA NA NA NA NA NA 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
[73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
[91] 91 92 93 94 95 96 97 98 99 100 NA

I don't understand where this NA is coming from at the end?
Original post by Mr XcX
Thank you for responding.

I'll try google tomorrow for more help on R I have a while before this is due.

So should I put my code as this,

c((x=1:100),(x[1:10]=NA)) x

with the x at the end. The wording is really confusing.


Yes, the wording is quite confusing.

It may be a good idea to have x on a seperate line, as typing that into one line on R won't work (as it won't know what to do inbetween the created vector and the x).
Original post by Mr XcX
When I input the code I get,

c((x=1:100),(x[1:10]=NA))
[1] NA NA NA NA NA NA NA NA NA NA 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
[73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
[91] 91 92 93 94 95 96 97 98 99 100 NA

I don't understand where this NA is coming from at the end?

Hm, I didn't originally spot that but right you are.

From the code you've created I don't understand why a 101st element is created and why it is NA. :dontknow:

If you create the vector in a slightly different way it doesn't get that problem, for whatever reason.

Spoiler

Reply 7
Original post by SeanFM
Yes, the wording is quite confusing.

It may be a good idea to have x on a seperate line, as typing that into one line on R won't work (as it won't know what to do inbetween the created vector and the x).


Thank you for helping me. I'll try that.
Original post by Mr XcX
Thank you for helping me. I'll try that.


I just saw your reply - see the post above :smile:
Reply 9
Original post by SeanFM
Hm, I didn't originally spot that but right you are.

From the code you've created I don't understand why a 101st element is created and why it is NA. :dontknow:

If you create the vector in a slightly different way it doesn't get that problem, for whatever reason.

Spoiler



I posted the code like this like you suggested, with the x on a separate line

c((x=1:100),(x[1:10]=NA))
x

and the NA 101'st value disappeared. We're using some software called Rteacher (not sure if you're familiar with it) to learn us how to use R and answer probability and stat questions. It's been glitchy as anything so would not surprise me if having the x on a separate line changed the vector.
(edited 8 years ago)
Original post by Mr XcX
I posted the code like this like you suggested, with the x on a separate line

c((x=1:100),(x[1:10]=NA))
x

and the NA 101'st value disappeared. We're using some software called Rteacher (not sure if you're familiar with it) to learn us how to use R and answer probability and stat questions. It's been glitchy as anything so would not surprise me if having the x on a separate line changed the vector.


I am using the regular version of R (I also have access to Rstudio but I'm not a huge fan of it) and I also got that error, maybe one day we'll know what it is as Maths students :smile:

But I'm glad that it's cleared up for you.
Reply 11
Original post by SeanFM
I am using the regular version of R (I also have access to Rstudio but I'm not a huge fan of it) and I also got that error, maybe one day we'll know what it is as Maths students :smile:

But I'm glad that it's cleared up for you.


Yeah, one day hopefully :smile:

Thanks again.

Quick Reply

Latest

Trending

Trending