在列表对象上使用R中的scale

时间:2015-03-03 02:34:17

标签: r

我在R中得到了这个列表对象,但不确定如何将其传递给比例

  

str(ss)
     chr [1:11600, 1:2] "1_1" "1_2" "1_3" "1_4" "1_5" "1_6" "1_7" "1_8" "1_9" "1_10" "1_11" "1_12" "1_13" "1_14" "1_15" ...
     - attr(*, "dimnames")=List of 2
      ..$ : chr [1:11600] "features" "features" "features" "features" ...
      ..$ : chr [1:2] "driver_trip" "prob"

    > ss[1:10,]
             driver_trip prob                
    features "1_1"       "0.0438400073093713"
    features "1_2"       "0.0750898149841077"
    features "1_3"       "0.108603234710245" 
    features "1_4"       "0.110244641673752" 
    features "1_5"       "0.114281674309826" 
    features "1_6"       "0.114693039193982" 
    features "1_7"       "0.11141782609152"  
    features "1_8"       "0.102231988595076" 
    features "1_9"       "0.1145982975793"   
    features "1_10"      "0.112394651156342" 
    > 

我尝试在0和0之间扩展概率。 1,中心0.5

> ss.prob2<-scale(ss[,2],center=0.5)
Error in FUN(x, aperm(array(STATS, dims[perm]), order(perm)), ...) : 
  non-numeric argument to binary operator

1 个答案:

答案 0 :(得分:0)

您的prob变量是字符。您可以转换为数据框,因为矩阵具有相同类型的元素(请注意,我只是使用rnorm作为示例):

> x <- matrix(c(paste0(1, "_", 1:10), rnorm(10)), ncol = 2)
> x
      [,1]   [,2]                
 [1,] "1_1"  "-0.764003122485116"
 [2,] "1_2"  "0.386744103161948" 
 [3,] "1_3"  "0.315941360908671" 
 [4,] "1_4"  "-1.17011716847999" 
 [5,] "1_5"  "-1.15084481612952" 
 [6,] "1_6"  "-0.42540116009025" 
 [7,] "1_7"  "0.243829019189806" 
 [8,] "1_8"  "-0.580361465305665"
 [9,] "1_9"  "-0.575600440871097"
[10,] "1_10" "0.633960614660247" 
> xdf <- as.data.frame(x, stringsAsFactors = FALSE)
> str(xdf)
'data.frame':   10 obs. of  2 variables:
 $ V1: chr  "1_1" "1_2" "1_3" "1_4" ...
 $ V2: chr  "-0.764003122485116" "0.386744103161948" "0.315941360908671" "-1.17011716847999" ...
> xdf$V2 <- as.numeric(xdf$V2)
> scale(xdf$V2, center = .5)
            [,1]
 [1,] -1.1745529
 [2,] -0.1052411
 [3,] -0.1710333
 [4,] -1.5519273
 [5,] -1.5340188
 [6,] -0.8599129
 [7,] -0.2380424
 [8,] -1.0039071
 [9,] -0.9994830
[10,]  0.1244806
attr(,"scaled:center")
[1] 0.5
attr(,"scaled:scale")
[1] 1.076157