简体   繁体   English

如何检查R中向量的变量是否为数字?

[英]How to check whether a variable is numeric for a vector in R?

I have two questions. 我有两个问题。

  for (k in 1:iterations) {
      corr <- cor(df2_prod[,k], df2_qa[,k])

      ifelse(is.numeric(corr), next,
              ifelse((all(df2_prod[,k] == df2_qa[,k])) ), (corr <- 1), (corr <- 0))

      correlation[k,] <- rbind(names(df2_prod[k]), corr)

    }

This is my requirement - I want to calculate correlation for variables in a loop using the code corr <- cor(df2_prod[,k], df2_qa[,k]) If i receive a correlation value in number, I have to keep the value as it is. 这是我的要求-我想使用代码corr <- cor(df2_prod[,k], df2_qa[,k])在循环中计算变量的相关性,如果我收到数量上的相关性值,则必须保持该值照原样。

Some time it happens that if two columns have the same values, i receive "NA" as output for the vector "corr". 有时,如果两列具有相同的值,我会收到“ NA”作为向量“ corr”的输出。

x   y
1   1
1   1
1   1
1   1
1   1

corr
     [,1]
[1,]   NA

I am trying to handle in such a way that if "NA" is received, i will replace the values with "1" or "0". 我试图以一种方式处理,如果收到“ NA”,我将用“ 1”或“ 0”替换值。

My questions are: 我的问题是:

  1. When I check the class of "corr" vector, I am getting it as "matrix". 当我检查“ corr”向量的类时,我将其作为“矩阵”。 I want to check whether that is a number or not. 我想检查一下这是否是数字。 Is there any other way other than checking is.numeric(corr) 除了检查is.numeric(corr)之外,还有其他方法吗

     > class(corr) [1] "matrix" 
  2. I want to check if two columns has same value or not. 我想检查两列是否具有相同的值。 Something like the code below. 类似于下面的代码。 If it returns true, I want to proceed. 如果返回true,我要继续。 But the way I have put the code in the loop is wrong. 但是我将代码放入循环的方式是错误的。 Could you please help me how this can be improved: ((all(df2_prod[,k] == df2_qa[,k])) 您能帮我改善一下吗: ((all(df2_prod[,k] == df2_qa[,k]))

Is there any effective way to do this? 有什么有效的方法可以做到这一点吗?

I sincerely apologize the readers for the poorly framed question / logic. 对于构架不佳的问题/逻辑,我深表歉意。 If you can show me pointers which can improve the code, I would be really thankful to you. 如果您可以向我展示可以改善代码的指针,我将非常感谢您。

1. You basically want to avoide NAs, right? 1.您基本上想避免使用NA,对吗? So you could check the result with is.na(). 因此,您可以使用is.na()检查结果。

a <- rep(1, 5)
b <- rep(1, 5)
if(is.na(cor(a, b))) cor.value <- 1

2.You could count how many times the element of a is equal to the element of b with sum(a==b) and check whether this amount is equal to the amount of elements in a (or b) --> length(a) 2,你可以用sum(a == b)计算a的元素等于b的元素多少次,并检查这个数量是否等于a(或b)-> length(一种)

if(sum(a==b) == length(a)) cor.value <- 1

An example to explain how the cor function works: 解释cor函数如何工作的示例:

set.seed(123)
df1 <- data.frame(v1=1:10, v2=rnorm(10), v3=rnorm(10), v4=rnorm(10))
df2 <- data.frame(w1=rnorm(10), w2=1:10, w3=rnorm(10))

Here, the first variable of df1 is equal to the second variable of df2 . 在此, df1的第一变量等于df2的第二变量。 Function cor directly applied on the first 3 variables of each data.frame gives: 直接在每个data.frame的前3个变量上应用的函数cor给出:

cor(df1[, 1:3], df2[, 1:3])
#           w1         w2         w3
#v1 -0.4603659  1.0000000  0.1078796
#v2  0.6730196 -0.2602059 -0.3486367
#v3  0.2713188 -0.3749826 -0.2520174

As you can notice, the correlation coefficient between w2 and v1 is 1 , not NA . 如您所见, w2v1之间的相关系数是1 ,而不是NA

So, in your case, cor(df2_prod[, 1:k], df2_qa[, 1:k]) should provide you the desired output. 因此,在您的情况下, cor(df2_prod[, 1:k], df2_qa[, 1:k])应该为您提供所需的输出。

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

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