R:二进制运算符的非数字参数 [英] R: non-numeric arguments to binary operators

查看:191
本文介绍了R:二进制运算符的非数字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R编程语言.我正在尝试绘制平行坐标图".使用一些虚假数据:

 库(MASS)一个= rnorm(100,10,10)b = rnorm(100、10、5)c = rnorm(100、5、10)d =矩阵(a,b,c)parcoord(d [,c(3,1,2)],col = 1 +(0:149)%/%50) 

但是,当我尝试将数字变量和因子变量混合在一起时会出现问题:

  group<-sample(LETTERS [1:4],100,replace = TRUE,prob = c(0.25,0.25,0.25,0.25))d =矩阵(a,b,组)parcoord(d [,c(3,1,2)],col = 1 +(0:149)%/%50)x中的错误-min(x,na.rm = TRUE):二进制运算符的非数字参数 

我很好奇.这个问题可以解决吗?还是根本不可能同时使用数字变量和因子变量来绘制这样的图?

我在这里看到了以前的stackoverflow帖子,其中使用数字和因子变量绘制了类似的图:

编辑

感谢您的补充说明.您想要的东西确实有意义,但是不幸的是,它看起来并不像我预期的那样起作用.我尝试使用有序因子作为中间变量来绘制图(每个

I am working with the R programming language. I am trying to make a "parallel coordinates plot" using some fake data:

library(MASS)

a = rnorm(100, 10, 10)

b = rnorm(100, 10, 5)

c = rnorm(100, 5, 10)

d = matrix(a, b, c)

parcoord(d[, c(3, 1, 2)], col = 1 + (0:149) %/% 50)

However, a problem arises when I try to mix numeric and factor variables together:

group <- sample( LETTERS[1:4], 100, replace=TRUE, prob=c(0.25, 0.25, 0.25, 0.25) )
d = matrix(a,b, group)
 parcoord(d[, c(3, 1, 2)], col = 1 + (0:149) %/% 50)

Error in x - min(x, na.rm = TRUE): non-numeric argument to binary operator

I am just curious. Can this problem be resolved? Or is it simply impossible to make such a plot using numeric and factor variables together?

I saw a previous stackoverflow post over here where a similar plot is made using numeric and factor variables: How to plot parallel coordinates with multiple categorical variables in R

However, I am using a computer with no USB port or internet access - I have a pre-installed version of R with limited libraries (I have plotly, ggplot2, dplyr, MASS ... I don't have ggally or tidyverse) and was looking for a way to do this only with the parcoord() function.

Does anyone have any ideas if this can be done? Thanks

Thanks

解决方案

One option is to label rows of the matrix using a factor and use that on the plot, e.g.

library(MASS)
set.seed(300)
par(xpd=TRUE)
par(mar=c(4, 4, 4, 6))
a = rnorm(12, 10, 10)
b = rnorm(12, 10, 5)
c = rnorm(12, 5, 10)
group <- sample(c("#FF9289", "#FF8AFF", "#00DB98", "#00CBFF"),
                12, replace=TRUE)

d = cbind(a, b, c)
rownames(d) <- group

parcoord(d[, c(3, 1, 2)], col = group)
title(main = "Plot", xlab = "Variable", ylab = "Values")
axis(side = 2, at = seq(0, 1, 0.1),
     tick = TRUE, las = 1)
legend(3.05, 1, legend = c("A", "B", "C", "D"), lty = 1,
       col = c("#FF9289", "#FF8AFF", "#00DB98", "#00CBFF"))

EDIT

Thanks for the additional explanation. What you want does make sense, but unfortunately it doesn't look like it will work as I expected. I tried to make a plot using an ordered factor as the middle variable (per https://pasteboard.co/JKK4AUD.jpg) but got the same error ("non-numeric argument to binary operator").

One way I thought of doing it is to recode the factor as a number (e.g. "Var_1" -> 0.2, "Var_2" -> 0.4) as below:

library(MASS)
set.seed(123)
par(xpd=TRUE)
par(mar=c(4, 4, 4, 6))
a = rnorm(12, 10, 10)
b = c(rep("Var_1", 3),
      rep("Var_2", 3),
      rep("Var_3", 3),
      rep("Var_4", 3))
c = rnorm(12, 5, 10)
group <- c(rep("#FF9289", 3),
           rep("#FF8AFF", 3),
           rep("#00DB98", 3),
           rep("#00CBFF", 3))

d = data.frame("A" = a,
               "Factor" = b,
               "C" = c,
               "Group" = group)

d$Factor <- sapply(d$Factor, switch,
                   "Var_1" = 0.8,
                   "Var_2" = 0.6,
                   "Var_3" = 0.4,
                   "Var_4" = 0.2)

parcoord(d[, c(1, 2, 3)], col = group)
title(main = "Plot", xlab = "Variable", ylab = "Values")
axis(side = 2, at = seq(0, 1, 0.1),
     tick = TRUE, las = 1)
legend(3.05, 1, legend = c("A", "B", "C", "D"), lty = 1,
       col = c("#FF9289", "#FF8AFF", "#00DB98", "#00CBFF"))
mtext(text = "Var 1", side = 1, adj = 0.6, padj = -30)
mtext(text = "Var 3", side = 1, adj = 0.6, padj = -12)
mtext(text = "Var 2", side = 1, adj = 0.6, padj = -21)
mtext(text = "Var 4", side = 1, adj = 0.6, padj = -3)

这篇关于R:二进制运算符的非数字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆