2013-04-26 79 views
1

我有以下数据框:错误切():“X”必须是数字

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 

什么我已经在改变“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] 

能有什么成为这个错误的原因?它如何被修复? 非常感谢您提前。

+0

请加dput的'输出(newdata.1)' – 2013-04-26 03:41:28

+0

我已经更新的问题,非常感谢你 – DSSS 2013-04-26 03:45:10

回答

5

这里是不是数字:

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

用途:

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

这绝对是太棒了!非常感谢你,马修伦德伯格! – DSSS 2013-04-26 03:57:22

+2

我应该已经发现了这个问题,但是用'options(error = recover)'找到它并运行你的代码。 – 2013-04-26 03:59:07

+0

选项(错误=恢复)似乎是伟大的事情,我想了解它是如何工作的 – DSSS 2013-04-26 04:04:30

相关问题