简体   繁体   中英

Spearman's Correlation Test: “Object not Found”

I have been using R for quite some time, and have used it to do all of the stats for my Master's thesis. I have one more thing to do before I am done with my thesis, and I cannot figure out why R is giving me this code.

I am trying to run a Spearman's correlation test, and have attached a CSV file into RStudio. Here is the code I have entered, and the error that R gives me:

littersize=read.csv(file.choose())
littersize
cor.test(littersize, progesterone, method = "spearman")

Error in cor.test.default(littersize, progesterone, method = "spearman") : object 'progesterone' not found

I have gotten this code before, and have been able to fix it without much issue, but for some reason it is just not working. I have checked spelling, I've checked the CSV file for errors, I have done everything I can think of. Can anyone help me figure out what R wants me to do??? Thanks!

Based on the code above (and complete lack of any data)it is hard to say.

However, you have not attached an object littersize into your rStudio work space.

You have loaded a data frame in your working environment assigning it the name littersize .

If you use

search()

you will see a list of everything you have attached; libraries, tools, your GlobalEnv... if "littersize" is not in there it is not attached.

This means that you cannot address columns in it directly.

So if progesterone is a column, of littersize then you must use one of the accepted method for doing so:

littersize$progesterone

or

with(littersize, cor.test(progesterone, x, method="spearman"))

Here progesterone and x are the two fields you are looking to test for correlations, both of which should be in data frame littersize if you intend to address it this way. And it is not clear if you have two separate fields to compare in your code (is one named littersize and one progesterone , both in the object littersize or did you load progesterone separately and not say or are you correlating one variable?).

If they are in different frames you must expressly call them with:

cor.test(littersize$progesterone, otherframe$x, method="spearman")

Or attach both objects and verify with search() and use the field names directly

If in fact the file is littersize and one of the columns is also littersize , then using attached addressing could become problematic, because you will have two objects of the same name attached, the frame and the column...in which case you should expressly ask for the columns with $ .

littersize$littersize

Or you could rename the attached file and you will need to detach littersize because changes to an attached object do not occur while it is attached.You would simply be adding a second copy with a new name, with the original with the original name still lingering causing problems.

detach(littersize)

In general, it is best NOT to attach files, to instead use a data frame with appropriate data frame addressing. And if you can, create naming paradigms which help avoid issues if there is redundancy, like:

  • data frame names always start with a capital Littersize
  • field/column/variable names are lower-case and dotted or underscored: litter_size or littersize or litter.size

  • objects in the environment always use camelCase: litterSize

If you are consistent about how you choose and use names, then you can have redundancy in an attached object and work through it, but it is probably still better to use a data frame and avoid the issues completely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM