PDA

View Full Version : R statistical: don't understand error message




Erniecranks
Apr 13, 2011, 01:43 PM
Here is my code:

> fp1c<-read.csv ('/Users/richardlerner/Desktop/feralpm1combo.csv', header=T)
> attach(fp1c)

And here is what got printed out:

The following object(s) are masked from 'package:datasets':

sleep

Sleep is one of the headers/variables

Any help, please?

ernie



kuwisdelu
Apr 13, 2011, 06:22 PM
That's not an error, just a warning or message telling you the following: in one of the default R packages called "datasets" there is also a variable called "sleep," and since you're using attach() on a different variable also called sleep, you won't be able to access the sleep variable in the "datasets" package in the same way anymore. When you ask for "sleep," R will use your version.

If you're familiar with namespaces or global variables vs. local variables in any other programming language, it's similar to that. If not, basically, it's telling you there are two things called "sleep," and since you've told R to use yours, the other one is now hidden or "masked."

I'd suggest not using the attach() function, and just accessing variables through the $ operator. Do fp1c$sleep instead, or use data=fp1c if you're using it in a function like lm() or something.

Erniecranks
Apr 15, 2011, 10:42 AM
Thank you, again. Your generosity is making my life much easier. I am making progress in my trying to plow through the online stuff, as well as two texts on R that I have bought. I wish they had taught R rather than SAS when I was in school. SAS is so hard to afford, that once people graduate they find themselves having learned something they can't use.

On to the next problem.

My data has been read in, using the attach command.

> socscore
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 3 2 3 3 1 5 10 0 0 0 0 0 0 0 0 0 0 0 0
[41] 0 1 1 2 5 5 5 8 8 10 10 10 0 0 0 0 0 2 2 8 0 0 0 0 0 0 0 0 0 1 2 2 6 5 5 8 9 8 10 0
[81] 0 6 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 1 2 3 5 5 5 7 5 7 5 6 5 6 6 5 9 8 9
[121] 9 8 8 9 10 10 10 10 10 10 10 10 10 10 10 10 10 2 10 0 0 0 0 2 6 5 5 9 8 9 10 10 10 10 10 10 0 2 2 3
[161] 5 4 5 7 9 9 9 9 10 10 10 10 10 10 10 10 0 2 2 6 5 5 5 6 5 4 5 8 9 9 8 9 9 8 9 9 10 10 10 10
[201] 10 10 10 10 8 8 9 10 3 7 5 6 6 5 8 9 9 8 9 10 10 10 10 10 10 10 10 0 6 10 10 10 10 10 10 10 10 6 7 7
[241] 9 10 10 0 9 8 8 8 9 8 10 10 10



This has about 250 values, 1 for each subject.

I then wrote this:

> if (any(socscore<=3)) q<-0 else q=1
> q
[1] 0
Why am I only getting 1 answer?

Thanks in advance,

Ernie

kuwisdelu
Apr 15, 2011, 12:58 PM
> if (any(socscore<=3)) q<-0 else q=1
> q
[1] 0
Why am I only getting 1 answer?

Thanks in advance,

Ernie

One of the hardest things to remember when using R compared to other programming languages, is that almost everything you do is a vector operation. When you do q <- 0, R is going to apply the "<- 0" to the entire vector of q. And if you haven't created q before this, q will just be set equal to either 0 or 1. What you're asking R to do is if any value in socscore is less or equal to than 3, assign 0 to q, otherwise assign 1 to q. I'm assuming you want q to be a vector of 0's and 1's corresponding to whether socscore <= 3 or not?

There are a couple ways to do that. You can either use a loop:

q <- rep(NA, length(socscore))
for ( i in 1:length(socscore) {
if ( socscore[i] <= 3 )
q[i] <- 0
else
q[i] <- 1
}

You need to use the [i] to tell R which element of socscore and q you're talking about, otherwise it'll assume you mean the entire vector of both.

But in general it's better to use vector operations in R rather than loops. The easier way to do this is:

q <- as.numeric(socscore > 3)

Here (socscore > 3) will result in a vector of booleans (true or false) of whether each element is greater than 3 or not and as.numeric() coerces the booleans to numerics as 0 or 1. Not again that any operation here applies to the whole vector. R will always operate on the whole vector unless you specifically ask for a particular index.

Again, I suggest not using attach(), but do data$socscore where data is whatever data.frame you read it into... but you'll eventually learn that on your own anyway if you keep using it, unless you stick to very simple stuff. ;)

Hansr
Apr 15, 2011, 01:55 PM
I recommend reading this: http://www.cyclismo.org/tutorial/R/ and if you want a really good guide this: http://stats.lse.ac.uk/penzer/ST419materials/