简体   繁体   English

Spearman的相关性测试:“找不到对象”

[英]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. 我使用R已有相当长的一段时间了,并用它来完成我硕士论文的所有统计。 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. 在完成论文之前,我还有另外一件事要做,我无法弄清楚为什么R给我这样的代码。

I am trying to run a Spearman's correlation test, and have attached a CSV file into RStudio. 我正在尝试运行Spearman的相关性测试,并将CSV文件附加到RStudio中。 Here is the code I have entered, and the error that R gives me: 这是我输入的代码,以及R给我的错误:

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 cor.test.default中的错误(littersize,孕酮,方法=“ spearman”):找不到对象“孕酮”

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. 我检查了拼写,检查了CSV文件中的错误,做了我能想到的一切。 Can anyone help me figure out what R wants me to do??? 谁能帮我弄清楚R想要我做什么??? 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. 但是,您尚未将对象littersize 附加到rStudio工作空间中。

You have loaded a data frame in your working environment assigning it the name littersize . 已经在工作环境中加载了一个数据框,并littersize指定了名称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. 库,工具,您的GlobalEnv ... 如果 "littersize" 不在其中,则不附加。

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: 因此,如果progesterone是一littersize的列,则必须使用一种可接受的方法:

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. 这里progesteronex是两个字段,你正在寻找测试的相关性,这两者应该是在数据帧littersize如果要解决这种方式。 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?). 尚不清楚您的代码中是否有两个要比较的字段(在对象littersize中是否是一个名为littersize和一个progesterone ,还是分别加载了progesterone而不是说还是在关联一个变量?)。

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 或附加两个对象并使用search()验证,然后直接使用字段名称

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 ,则使用附加寻址可能会出现问题,因为您将附加两个具有相同名称的对象(框架和列)……在这种情况下,您应该明确要求$的列。

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. 或者,您可以重命名附件文件,并且需要分离littersize因为在附件附加对象时不会对附件对象进行更改。您只需添加第二个新名称副本,而原始名称仍会挥之不去造成问题。

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 数据框名称始终以大写Littersize
  • field/column/variable names are lower-case and dotted or underscored: litter_size or littersize or litter.size 字段/列/变量名称为小写字母,并litter_size点划线或下划线: litter_sizelittersizelitter.size

  • objects in the environment always use camelCase: litterSize 环境中的对象始终使用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. 如果您对选择和使用名称的方式持一致态度,则可以在附加对象中进行冗余处理,但最好还是使用数据框并完全避免出现此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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