警告:在使用四分位数字符替换数字列表中的数字时,通过强制引入的NA

时间:2014-09-11 14:54:35

标签: r

我是R的新手,并试图搜索强制错误引入的类似NA,但在我的情况下,我在运行以下代码时收到此警告,尽管它看起来似乎有效:

  

x <-c(0.0526,0.1068,0.0949,0.0475,-0.3409,-0.0274,0.1607,0.0964,-0.0340,0.0093,0.0504,0.1549,0.0386,-0.0557,-0.0754,0.0141,0.1151,0.2173,0.1324 ,-0.0574,-0.0484,0.0197,-0.0458,-0.0586,-0.0137,-0.0821,0.0877,-0.0668,-0.0871,-0.1810,-0.2832,-0.1525,0.0211,-0.1331,-0.1719,0.1391,0.0941, -0.0341,-0.1839,0.1012,-0.1970,-0.1037,-0.0964,-0.1366,-0.0958,-0.1717,0.1155,-0.0472,0.2427,0.0191,0.0476,0.1306,0.0712,0.1148,0.1651,-0.0431,-0.0183 ,-0.1063,0.0607,0.1499,0.0257,-0.0482,-0.0832,0.0487,-0.0318,-0.0488,-0.1711,0.1053,0.1202,0.0715,0.1258,-0.0191,0.0946,0.0607,0.0826,0.0613,-0.0113)< / p>

我正在尝试使用以下命令将x的值替换为该数字所属的四分位数范围的字符:

     quartiles<-quantile(x,na.rm=TRUE)
    (x<-as.list(x))
    (x[x>=quartiles[1] & x<quartiles[2]] <-paste("Quartile 1: ",quartiles[1],"-",quartiles[2],sep=""))
    (x[x>=quartiles[2] & x<quartiles[3]] <-paste("Quartile 2: ",quartiles[2],"-",quartiles[3],sep=""))
    (x[x>=quartiles[3] & x<quartiles[4]] <-paste("Quartile 3: ",quartiles[3],"-",quartiles[4],sep=""))
    (x[x>=quartiles[4] & x<=quartiles[5]]<-paste("Quartile 4: ",quartiles[4],"-",quartiles[5],sep=""))
    x<-unlist(x)
    x

我试图在四分位数之间选择X中的值(即Q1的1和2)并用它们各自的四分位数字符替换它们的步骤是我得到以下警告打印输出的地方:

Q1命令:没有错误?

Q2命令:

There were 38 warnings (use warnings() to see them)

Q3命令:

There were 50 or more warnings (use warnings() to see them)

Q4命令:

There were 50 or more warnings (use warnings() to see them)

警告都是以下类型:

1: In x[x >= quartiles[4] & x <= quartiles[5]] <- paste("Quartile 4: ",  ... :
NAs introduced by coercion

尽管如此,似乎新的x向量在任何地方都不包含任何NAs,并且每个值都被替换为相应的Quartile和范围。

在使用as.list(x)之前,我尝试使用以下方法执行相同的过程:

quartiles<-quantile(x,na.rm=TRUE)
    (x[x>=quartiles[1] & x<quartiles[2]] <-paste("Quartile 1: ",quartiles[1],"-",quartiles[2],sep=""))
    (x[x>=quartiles[2] & x<quartiles[3]] <-paste("Quartile 2: ",quartiles[2],"-",quartiles[3],sep=""))
    (x[x>=quartiles[3] & x<quartiles[4]] <-paste("Quartile 3: ",quartiles[3],"-",quartiles[4],sep=""))
    (x[x>=quartiles[4] & x<=quartiles[5]]<-paste("Quartile 4: ",quartiles[4],"-",quartiles[5],sep=""))
    x

这种方法有效,但所有值都没有正确替换,即18个Q2值保持不变(尽管一个Q2值改变了(x [22])。

  

X    [1]&#34;四分位数3:0.0141-0.0946&#34; &#34;四分位数4:0.0946-0.2427&#34; &#34;四分位数4:0.0946-0.2427&#34; &#34;四分位数3:0.0141-0.0946&#34;
   [5]&#34;四分位数1:-0.3409--0.0668&#34; &#34; -0.0274&#34; &#34;四分位数4:0.0946-0.2427&#34; &#34;四分位数4:0.0946-0.2427&#34;
   [9]&#34; -0.034&#34; &#34;四分位数2:-0.0668-0.0141&#34; &#34;四分位数3:0.0141-0.0946&#34; &#34;四分位数4:0.0946-0.2427&#34;
  [13]&#34;四分位数3:0.0141-0.0946&#34; &#34; -0.0557&#34; &#34;四分位数1:-0.3409--0.0668&#34; &#34;四分位数3:0.0141-0.0946&#34;
  [17]&#34;四分位数4:0.0946-0.2427&#34; &#34;四分位数4:0.0946-0.2427&#34; &#34;四分位数4:0.0946-0.2427&#34; &#34; -0.0574&#34;
  [21]&#34; -0.0484&#34; &#34;四分位数3:0.0141-0.0946&#34; &#34; -0.0458&#34; &#34; -0.0586&#34;
  [25]&#34; -0.0137&#34; &#34;四分位数1:-0.3409--0.0668&#34; &#34;四分位数3:0.0141-0.0946&#34; &#34;四分位数2:-0.0668-0.0141&#34;   [29]&#34;四分位数1:-0.3409--0.0668&#34; &#34;四分位数1:-0.3409--0.0668&#34; &#34;四分位数1:-0.3409--0.0668&#34; &#34;四分位数1:-0.3409--0.0668&#34;   [33]&#34;四分位数3:0.0141-0.0946&#34; &#34;四分位数1:-0.3409--0.0668&#34; &#34;四分位数1:-0.3409--0.0668&#34; &#34;四分位数4:0.0946-0.2427&#34;
  [37]&#34;四分位数3:0.0141-0.0946&#34; &#34; -0.0341&#34; &#34;四分位数1:-0.3409--0.0668&#34; &#34;四分位数4:0.0946-0.2427&#34;
  [41]&#34;四分位数1:-0.3409--0.0668&#34; &#34;四分位数1:-0.3409--0.0668&#34; &#34;四分位数1:-0.3409--0.0668&#34; &#34;四分位数1:-0.3409--0.0668&#34;   [45]&#34;四分位数1:-0.3409--0.0668&#34; &#34;四分位数1:-0.3409--0.0668&#34; &#34;四分位数4:0.0946-0.2427&#34; &#34; -0.0472&#34;
  [49]&#34;四分位数4:0.0946-0.2427&#34; &#34;四分位数3:0.0141-0.0946&#34; &#34;四分位数3:0.0141-0.0946&#34; &#34;四分位数4:0.0946-0.2427&#34;
  [53]&#34;四分位数3:0.0141-0.0946&#34; &#34;四分位数4:0.0946-0.2427&#34; &#34;四分位数4:0.0946-0.2427&#34; &#34; -0.0431&#34;
  [57]&#34; -0.0183&#34; &#34;四分位数1:-0.3409--0.0668&#34; &#34;四分位数3:0.0141-0.0946&#34; &#34;四分位数4:0.0946-0.2427&#34;
  [61]&#34;四分位数3:0.0141-0.0946&#34; &#34; -0.0482&#34; &#34;四分位数1:-0.3409--0.0668&#34; &#34;四分位数3:0.0141-0.0946&#34;
  [65]&#34; -0.0318&#34; &#34; -0.0488&#34; &#34;四分位数1:-0.3409--0.0668&#34; &#34;四分位数4:0.0946-0.2427&#34;
  [69]&#34;四分位数4:0.0946-0.2427&#34; &#34;四分位数3:0.0141-0.0946&#34; &#34;四分位数4:0.0946-0.2427&#34; &#34; -0.0191&#34;
  [73]&#34;四分位数4:0.0946-0.2427&#34; &#34;四分位数3:0.0141-0.0946&#34; &#34;四分位数3:0.0141-0.0946&#34; &#34;四分位数3:0.0141-0.0946&#34;
  [77]&#34; -0.0113&#34;

我认为这与执行第一个替换命令时更改的整个向量的类有关,因此我使用as.list(x)认为列表可以包含不同类的对象。

无论如何,知道发生了什么,是否有更好的方法来尝试完成这项任务?我可以抑制警告()替换命令,一切似乎都有效,但我真的想提高自己的能力,并在将来避免这些问题。谢谢!

1 个答案:

答案 0 :(得分:1)

问题在于您将字符分配给数字变量,您不应该这样做。

以下是使用cut函数的更好解决方案,该函数专为此设计:

y <- cut(x, quartiles,  include.lowest=TRUE, right=FALSE)
levels(y) <- paste0("Quartile ", 1:4, ": ", levels(y))

head(y)

#[1] Quartile 3: [0.0141,0.0946)  Quartile 4: [0.0946,0.243]   Quartile 4: [0.0946,0.243]   Quartile 3: [0.0141,0.0946) 
#[5] Quartile 1: [-0.341,-0.0668) Quartile 2: [-0.0668,0.0141)
#Levels: Quartile 1: [-0.341,-0.0668) Quartile 2: [-0.0668,0.0141) Quartile 3: [0.0141,0.0946) Quartile 4: [0.0946,0.243]