cut()出错:'x'必须是数字

时间:2013-04-26 03:32:08

标签: r

我有以下数据框:

   newdata.1
      a.0    b.0    c.0   a.1  b.1    c.1   a.3    b.3    c.3
  1  40.00 100.00  77.78  NA  88.89  97.22    NA  80.56 100.00
  2 100.00 100.00 100.00 100 100.00 100.00    NA 100.00 100.00
  3  50.00 100.00  75.00  75 100.00  86.11    NA     NA     NA
  4  66.67  59.38 100.00  NA  59.38 100.00    NA  65.63 100.00
  5  37.50 100.00  69.44  NA 100.00  94.44 41.67  83.33  63.89
  6 100.00 100.00  91.67 100 100.00  94.44 75.00 100.00  91.67

 dput(newdata.1) 

 structure(list(a.0 = c(40, 100, 50, 66.67, 37.5, 100), b.0 = c(100, 
100, 100, 59.38, 100, 100), c.0 = c(77.78, 100, 75, 100, 69.44, 
91.67), a.1 = c(NA, 100, 75, NA, NA, 100), b.1 = c(88.89, 100, 
100, 59.38, 100, 100), c.1 = c(97.22, 100, 86.11, 100, 94.44, 
94.44), a.3 = c(NA, NA, NA, NA, 41.67, 75), b.3 = c(80.56, 100, 
NA, 65.63, 83.33, 100), c.3 = c(100, 100, NA, 100, 63.89, 91.67
)), .Names = c("a.0", "b.0", "c.0", "a.1", "b.1", "c.1", "a.3", 
"b.3", "c.3"), class = "data.frame", row.names = c(NA, 6L))
>

当我将以下代码应用于此数据框时,它运行良好:

 vars<-c("a","b","c")

  # Code 1
 res <- lapply(vars ,function(i) {
 breaks <- c(-Inf,unique(quantile(newdata.1 [,paste(i,1,sep=".")], na.rm=T)),Inf)
 cut(newdata.1[,paste(i, 1 ,sep=".")],breaks, na.rm=T)
  })

 > res
 [[1]]
 [1] <NA>       (87.5,100] (-Inf,75]  <NA>       <NA>       (87.5,100]
 Levels: (-Inf,75] (75,87.5] (87.5,100] (100, Inf]

 [[2]]
 [1] (59.4,91.7] (91.7,100]  (91.7,100]  (-Inf,59.4] (91.7,100]  (91.7,100] 
 Levels: (-Inf,59.4] (59.4,91.7] (91.7,100] (100, Inf]

 [[3]]
 [1] (95.8,99.3] (99.3,100]  (-Inf,86.1] (99.3,100]  (86.1,94.4] (86.1,94.4]
 Levels: (-Inf,86.1] (86.1,94.4] (94.4,95.8] (95.8,99.3] (99.3,100] (100, Inf]

但是在稍微修改代码之后,它就不起作用了

#Code 2
res <- lapply(vars ,function(i) {
breaks <- c(-Inf,unique(quantile(newdata.1 [,paste(i,1,sep=".")], na.rm=T)),Inf)
cut(newdata.1[,paste(i, 0:1 ,sep=".")],breaks, na.rm=T)
})

Error in cut.default(newdata.1[, paste(i, 0:1, sep = ".")], breaks, na.rm = T) : 
 'x' must be numeric

我所做的是在最后一行的cut()中的paste()中将“1”更改为“0:1”

当我将两种代码变体应用于非常相似但人为的数据帧时,一切都很有效:

 varnames<-c( "a.0", "b.0", "c.0", "a.1", "b.1", "c.1", "a.3", "b.3", "c.3")

  a <-matrix (c(1,2,3,4, 5, 6, 7, 8, 9), 2, 9)

  colnames (a)<-varnames

  df<-as.data.frame (a)

  a
      a.0 b.0 c.0 a.1 b.1 c.1 a.3 b.3 c.3
 [1,]   1   3   5   7   9   2   4   6   8
 [2,]   2   4   6   8   1   3   5   7   9

#Code 1
 res <- lapply(vars ,function(i) {
 breaks <- c(-Inf,unique(quantile(a [,paste(i,1,sep=".")], na.rm=T)),Inf)
 cut(a[,paste(i, 1 ,sep=".")],breaks, na.rm=T)
 })

res
[[1]]
[1] (-Inf,7] (7.75,8]
Levels: (-Inf,7] (7,7.25] (7.25,7.5] (7.5,7.75] (7.75,8] (8, Inf]

[[2]]
[1] (7,9]    (-Inf,1]
Levels: (-Inf,1] (1,3] (3,5] (5,7] (7,9] (9, Inf]

[[3]]
[1] (-Inf,2] (2.75,3]
 Levels: (-Inf,2] (2,2.25] (2.25,2.5] (2.5,2.75] (2.75,3] (3, Inf]

 #Code 2
  res <- lapply(vars ,function(i) {
 breaks <- c(-Inf,unique(quantile(a [,paste(i,1,sep=".")], na.rm=T)),Inf)
 cut(a[,paste(i, 0:1 ,sep=".")],breaks, na.rm=T)
 })

 res
 [[1]]
 [1] (-Inf,7] (-Inf,7] (-Inf,7] (7.75,8]
 Levels: (-Inf,7] (7,7.25] (7.25,7.5] (7.5,7.75] (7.75,8] (8, Inf]

 [[2]]
 [1] (1,3]    (3,5]    (7,9]    (-Inf,1]
 Levels: (-Inf,1] (1,3] (3,5] (5,7] (7,9] (9, Inf]

 [[3]]
 [1] (3, Inf] (3, Inf] (-Inf,2] (2.75,3]
 Levels: (-Inf,2] (2,2.25] (2.25,2.5] (2.5,2.75] (2.75,3] (3, Inf]

出现此错误的原因是什么?怎么修好? 非常感谢你提前。

1 个答案:

答案 0 :(得分:6)

这不是数字:

class(newdata.1[,1])
## [1] "numeric"
class(newdata.1[,1:2])
## [1] "data.frame"

使用:

as.matrix(newdata.1[,paste(i, 0:1 ,sep=".")])