R功能不能做我想做的事

时间:2014-06-03 10:19:16

标签: r

我不明白为什么我的功能不起作用,你能不能帮我找到我的错误。

VehiculeFunction <- function(data){
  my.data <- data[data$GAMME =="M1",]
  ma.col = rgb(red = 0.1,blue = 1,green = 0.1, alpha = 0.2)
  X <- my.data$GMF.24
  Y <- my.data$Cout.24
  X11()
  plot(X, Y, pch=20, las = 1, col = ma.col, xlim = c(0, 10), ylim = c(0,10))
  identify(X, Y, labels = my.data$NITG, cex = 0.7)
}

这个工作完美,当我添加两个变量时,它返回&#34;数字(0)&#34;

VehiculeFunction <- function(data, x, y){
  my.data <- data[data$GAMME =="M1",]
  ma.col = rgb(red = 0.1,blue = 1,green = 0.1, alpha = 0.2)
  X <- my.data$x
  Y <- my.data$y
  X11()
  plot(X, Y, pch=20, las = 1, col = ma.col, xlim = c(0, 10), ylim = c(0,10))
  identify(X, Y, labels = my.data$NITG, cex = 0.7)
}
VehiculeFunction(data.vehicule, GMF.24, Cout.24)
numeric(0)

谢谢Oleg,如果我想在我的数据帧的另一个变量上添加一个条件,我想先取三个&#34; RANG_NITG_PROJET_K&#34;

my.data1 <- my.data[my.data$RANG_NITG_PROJET_K == 1|2|3,] ? 

但是|认为这是假的,因为当我这样做时

my.data1 <- data.vehicule[data.vehicule$RANG_NITG_PROJET_K == 1,]
my.data2 <- data.vehicule[data.vehicule$RANG_NITG_PROJET_K == 2,]
my.data3 <- data.vehicule[data.vehicule$RANG_NITG_PROJET_K == 3,]
my.data <- rbind(my.data1, my.data2, my.data3)

它给了我两个不同的数据框?

1 个答案:

答案 0 :(得分:2)

尝试以下方法。在这种情况下,您无法使用$运算符访问my.data列(或列表元素),并且需要为xy传递字符串:

VehiculeFunction <- function(data, x, y, gamme){
  my.data <- data[data$GAMME == gamme,]
  ma.col = rgb(red = 0.1,blue = 1,green = 0.1, alpha = 0.2)
  X <- my.data[[x]] # <- change from $ to [[]]
  Y <- my.data[[y]] # <- change from $ to [[]]
  X11()
  plot(X, Y, pch=20, las = 1, col = ma.col, xlim = c(0, 10), ylim = c(0,10))
  identify(X, Y, labels = my.data$NITG, cex = 0.7)
}

VehiculeFunction(data.vehicule, "GMF.24", "Cout.24", "M1") # <- change to strings