Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 13, 2011, 01:43 PM   #1
Erniecranks
macrumors newbie
 
Join Date: Apr 2011
R statistical: don't understand error message

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
Erniecranks is offline   0 Reply With Quote
Old Apr 13, 2011, 06:22 PM   #2
kuwisdelu
macrumors 65816
 
Join Date: Jan 2008
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.
__________________
2.0 GHz quad-core i7 15" MacBook Pro 2011
iPad 2 (Black) WiFi+3G 64GB
iPhone 4S (White) 64 GB
kuwisdelu is offline   0 Reply With Quote
Old Apr 15, 2011, 10:42 AM   #3
Erniecranks
Thread Starter
macrumors newbie
 
Join Date: Apr 2011
thanks again, more questions!

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
Erniecranks is offline   0 Reply With Quote
Old Apr 15, 2011, 12:58 PM   #4
kuwisdelu
macrumors 65816
 
Join Date: Jan 2008
Quote:
Originally Posted by Erniecranks View Post
> 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:
Code:
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:

Code:
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.
__________________
2.0 GHz quad-core i7 15" MacBook Pro 2011
iPad 2 (Black) WiFi+3G 64GB
iPhone 4S (White) 64 GB
kuwisdelu is offline   0 Reply With Quote
Old Apr 15, 2011, 01:55 PM   #5
Hansr
macrumors 6502a
 
Join Date: Apr 2007
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/
Hansr is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
General: I don't understand something rrares1996 iOS 7 15 Sep 3, 2013 07:52 AM
I don't understand why Lovejess47 iPhone 7 Sep 25, 2012 05:25 PM
I don't understand passbook? stevo505 iOS 6 0 Sep 19, 2012 07:43 PM
Don't quite understand? splatterific iMac 4 Jul 31, 2012 10:45 PM
I don't understand.. leadfeather89 iMac 7 Jun 10, 2012 09:14 AM

Forum Jump

All times are GMT -5. The time now is 12:34 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC