2015-03-03 73 views
0

我獲得了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概率比例& 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 

回答

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