设为首页 收藏本站
查看: 872|回复: 0

[经验分享] python查找指定数据,用R绘图

[复制链接]

尚未签到

发表于 2018-8-15 12:38:48 | 显示全部楼层 |阅读模式
> mean  
function (x, ...)
  
UseMethod("mean")
  
<bytecode: 0x00000000111edea8>
  
<environment: namespace:base>
  

  

  
> mean.default
  
function (x, trim = 0, na.rm = FALSE, ...)
  
{
  
    if (!is.numeric(x) && !is.complex(x) && !is.logical(x)) {
  
        warning("argument is not numeric or logical: returning NA")
  
        return(NA_real_)
  
    }
  
    if (na.rm)
  
        x <- x[!is.na(x)]
  
    if (!is.numeric(trim) || length(trim) != 1L)
  
        stop("'trim' must be numeric of length one")
  
    n <- length(x)
  
    if (trim > 0 && n) {
  
        if (is.complex(x))
  
            stop("trimmed means are not defined for complex data")
  
        if (anyNA(x))
  
            return(NA_real_)
  
        if (trim >= 0.5)
  
            return(stats::median(x, na.rm = FALSE))
  
        lo <- floor(n * trim) + 1
  
        hi <- n + 1 - lo
  
        x <- sort.int(x, partial = unique(c(lo, hi)))[lo:hi]
  
    }
  
    .Internal(mean(x))
  
}
  
<bytecode: 0x00000000111ee3c0>
  
<environment: namespace:base>
  

  
/////////////////////////////////////////////////////////////////////
  

  

  
> mt <- function(v){
  
+     n <- length(v)
  
+     t1 <- v[1:5]
  
+     tp <- sum(v[6:n])
  
+     t2 <- v[6:n]
  
+     if (tp > 35){
  
+         max_index = which(t2==max(t2))[1]
  
+         t2[max_index] = max(t2)- tp + 35
  
+     }
  
+     return(c(t1,t2))
  
+ }
  

  
> mt(1:10)
  
[1] 1 2 3 4 5 6 7 8 9 5
  

  
> a <- 1:10
  
> b <- c(2,5,6,7, 466, 466,466, 34,5,2)
  
> c <- c(2,5,6,7, 466, 89,14, 56,7,2)
  
> data1 <- data.frame(a,b,c)
  
> data1
  
    a   b   c
  
1   1   2   2
  
2   2   5   5
  
3   3   6   6
  
4   4   7   7
  
5   5 466 466
  
6   6 466  89
  
7   7 466  14
  
8   8  34  56
  
9   9   5   7
  
10 10   2   2
  

  
> apply(data1, 2, mt)
  
      a    b   c
  
[1,] 1    2   2
  
[2,] 2    5   5
  
[3,] 3    6   6
  
[4,] 4    7   7
  
[5,] 5  466 466
  
[6,] 6 -472 -44
  
[7,] 7  466  14
  
[8,] 8   34  56
  
[9,] 9    5   7
  
[10,] 5    2   2
  

  
> data1 <- data.frame(rbind(a,b,c) )
  
> data1
  
  X1 X2 X3 X4  X5  X6  X7 X8 X9 X10
  
a  1  2  3  4   5   6   7  8  9  10
  
b  2  5  6  7 466 466 466 34  5   2
  
c  2  5  6  7 466  89  14 56  7   2
  

  
> apply(data1, 1, mt)
  
    a    b   c
  
X1  1    2   2
  
X2  2    5   5
  
X3  3    6   6
  
X4  4    7   7
  
X5  5  466 466
  
X6  6 -472 -44
  
X7  7  466  14
  
X8  8   34  56
  
X9  9    5   7
  
X10 5    2   2
  

  
///////////////////////////////////////////////////////////////////////
  

  
> data <- read.table("test_plot.txt", skip=1, stringsAsFactors =F)
  

  
> c("A","B",3)
  
[1] "A" "B" "3"
  

  
> mt <- function(v){
  
       n <- length(v)
  
       t1 <- v[1:5]
  
       t2 <- as.numeric(v[6:n])
  
       tp <- sum(t2)
  

  
        if (tp > 1){
  
             max_index = which(t2==max(t2))[1]
  
           t2[max_index] = max(t2)- tp + 1
  
         } else if (tp < 1){
  
             min_index = which(t2==min(t2))[1]
  
             t2[min_index] = min(t2) + 1 - tp
  
         }
  
         return(c(t1,t2))
  
   }
  

  
>
  
> apply(data[1:3,], 1, mt)
  
   1       2       3
  
V1 "1"     "2"     "3"
  
V2 "100"   "101"   "102"
  
V3 "(0)"   "(0)"   "(0)"
  
V4 "1"     "1"     "1"
  
V5 ":"     ":"     ":"
  
   "0.663" "0.767" "0.651"
  
   "0.185" "0.042" "0.083"
  
   "0.039" "0.033" "0.062"
  
   "0.113" "0.158" "0.204"
  

  

  
> t(apply(data, 1, mt))
  
       V1    V2    V3    V4  V5
  
  [1,] "  1" "100" "(0)" "1" ":" "0.663"              "0.185"
  
  [2,] "  2" "101" "(0)" "1" ":" "0.767"              "0.042"
  
  [3,] "  3" "102" "(0)" "1" ":" "0.651"              "0.083"
  
  [4,] "  4" "103" "(0)" "1" ":" "0.358"              "0.078"
  
  [5,] "  5" "104" "(0)" "1" ":" "0.062"              "0.096"
  
  [6,] "  6" "105" "(0)" "1" ":" "0.058"              "0.098"
  

  

  

  
> t <- sprintf( "%.7f", 1.44*1.21)
  
> t
  
[1] "1.7424000"
  
> t <- sprintf( "%.3f", 1.44*1.21)
  
> t
  
[1] "1.742"
  

  
> mt <- function(v){
  
+        n <- length(v)
  
+        t1 <- v[1:5]
  
+        t2 <- as.numeric(v[6:n])
  
+        tp <- sum(t2)
  
+
  
+         if (tp > 1){
  
+              max_index <- which(t2==max(t2))[1]
  
+              t2[max_index] <- sprintf("%.3f", max(t2)-tp+1)
  
+         } else if (tp < 1){
  
+              min_index <- which(t2==min(t2))[1]
  
+              t2[min_index] <- sprintf("%.3f", min(t2) + 1 - tp)
  
+         }
  
+         return(c(t1,t2))
  
+ }
  

  

  
> head(data)
  
  V1  V2  V3 V4 V5    V6    V7    V8    V9
  
1  1 100 (0)  1  : 0.663 0.185 0.038 0.113
  
2  2 101 (0)  1  : 0.767 0.042 0.033 0.158
  
3  3 102 (0)  1  : 0.651 0.083 0.062 0.204
  
4  4 103 (0)  1  : 0.358 0.078 0.185 0.379
  
5  5 104 (0)  1  : 0.062 0.096 0.487 0.355
  
6  6 105 (0)  1  : 0.058 0.098 0.516 0.329
  

  
> head(data[,6:9])
  
     V6    V7    V8    V9
  
1 0.663 0.185 0.038 0.113
  
2 0.767 0.042 0.033 0.158
  
3 0.651 0.083 0.062 0.204
  
4 0.358 0.078 0.185 0.379
  
5 0.062 0.096 0.487 0.355
  
6 0.058 0.098 0.516 0.329
  

  

  
> head(t(apply(data, 1, mt)))
  
     V1    V2    V3    V4  V5
  
[1,] "  1" "100" "(0)" "1" ":" "0.663" "0.185" "0.039" "0.113"
  
[2,] "  2" "101" "(0)" "1" ":" "0.767" "0.042" "0.033" "0.158"
  
[3,] "  3" "102" "(0)" "1" ":" "0.651" "0.083" "0.062" "0.204"
  
[4,] "  4" "103" "(0)" "1" ":" "0.358" "0.078" "0.185" "0.379"
  
[5,] "  5" "104" "(0)" "1" ":" "0.062" "0.096" "0.487" "0.355"
  
[6,] "  6" "105" "(0)" "1" ":" "0.058" "0.098" "0.515" "0.329"
  

  
> apply(data[,6:9], 1 , sum)
  
  [1] 0.999 1.000 1.000 1.000 1.000 1.001 1.000 1.000 1.000 1.000 1.000 1.000
  
[13] 1.001 1.000 1.000 1.001 0.999 0.999 1.000 1.000 1.000 1.000 1.000 1.000
  
[25] 1.001 1.000 1.001 1.000 0.999 1.000 1.000 1.000 1.000 1.000 1.001 1.000
  
[37] 1.000 1.000 0.999 1.000 0.999 1.001 1.000 0.999 1.000 1.000 1.000 1.000
  
[49] 1.000 0.999 1.000 1.000 1.001 1.000 1.001 1.000 1.000 1.001 1.000 1.000
  
[61] 1.000 1.000 1.000 1.001 0.999 1.000 0.999 1.001 0.999 0.999 1.000 1.000
  
[73] 0.999 1.000 1.000 1.000 0.999 1.000 1.000 1.001 1.000 1.000 1.000 1.001
  
[85] 1.000 1.001 1.000 1.000 1.000 1.001 1.000 1.000 1.000 1.000 1.000 1.001
  
[97] 1.001 1.000 1.000 1.000 1.000 1.000 1.001 1.001 1.000 1.001 1.000 1.000
  
[109] 1.000 1.000 0.999 0.999 1.001 1.000 1.000 1.000 0.999 1.001 1.000 1.000
  
[121] 1.000 1.000 1.000 1.000 1.000 0.999 1.000 1.000 1.000 1.000 1.000 1.000
  
[133] 1.000 1.001 1.001 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000
  
[145] 1.001 0.999 1.000 0.999 1.000 1.000 0.999 1.000 0.999 1.000 0.999 1.000
  
[157] 0.999 0.999 1.000 1.001 1.000 1.001 1.000 1.000 1.000 0.999 1.000 1.001
  
[169] 1.000 1.000 0.999 1.000 1.000 1.000 0.999
  

  
> x <- t(apply(data, 1, mt))
  

  
> head(x)
  
     V1    V2    V3    V4  V5
  
[1,] "  1" "100" "(0)" "1" ":" "0.663" "0.185" "0.039" "0.113"
  
[2,] "  2" "101" "(0)" "1" ":" "0.767" "0.042" "0.033" "0.158"
  
[3,] "  3" "102" "(0)" "1" ":" "0.651" "0.083" "0.062" "0.204"
  
[4,] "  4" "103" "(0)" "1" ":" "0.358" "0.078" "0.185" "0.379"
  
[5,] "  5" "104" "(0)" "1" ":" "0.062" "0.096" "0.487" "0.355"
  
[6,] "  6" "105" "(0)" "1" ":" "0.058" "0.098" "0.515" "0.329"
  

  

  
> head(x[,6:9])
  

  
[1,] "0.663" "0.185" "0.039" "0.113"
  
[2,] "0.767" "0.042" "0.033" "0.158"
  
[3,] "0.651" "0.083" "0.062" "0.204"
  
[4,] "0.358" "0.078" "0.185" "0.379"
  
[5,] "0.062" "0.096" "0.487" "0.355"
  
[6,] "0.058" "0.098" "0.515" "0.329"
  

  

  
> m  <- apply(data.frame(x[,6:9]), 1, as.numeric)
  

  
> apply(m,2,sum)
  

  
  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  
[37] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  
[73] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  
[109] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  
[145] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  

  
> n  <- apply(data.frame(x[,6:9]), 2, as.numeric)
  

  
> apply(n,1,sum)
  
  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  
[37] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  
[73] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  
[109] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  
[145] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  
>
  

  
/////////////////////////////////////////////////////////////////////
  

  
> x <- t(apply(data, 1, mt))
  
> head(x)
  
     V1    V2    V3    V4  V5
  
[1,] "  1" "100" "(0)" "1" ":" "0.663" "0.185" "0.039" "0.113"
  
[2,] "  2" "101" "(0)" "1" ":" "0.767" "0.042" "0.033" "0.158"
  
[3,] "  3" "102" "(0)" "1" ":" "0.651" "0.083" "0.062" "0.204"
  
[4,] "  4" "103" "(0)" "1" ":" "0.358" "0.078" "0.185" "0.379"
  
[5,] "  5" "104" "(0)" "1" ":" "0.062" "0.096" "0.487" "0.355"
  
[6,] "  6" "105" "(0)" "1" ":" "0.058" "0.098" "0.515" "0.329"
  

  
> x1 <- as.data.frame(x)
  
> head(x1)
  
   V1  V2  V3 V4 V5    V6    V7    V8    V9
  
1   1 100 (0)  1  : 0.663 0.185 0.039 0.113
  
2   2 101 (0)  1  : 0.767 0.042 0.033 0.158
  
3   3 102 (0)  1  : 0.651 0.083 0.062 0.204
  
4   4 103 (0)  1  : 0.358 0.078 0.185 0.379
  
5   5 104 (0)  1  : 0.062 0.096 0.487 0.355
  
6   6 105 (0)  1  : 0.058 0.098 0.515 0.329
  

  
> data_p <- x1[order(x1[,6], decreasing=T),]
  

  
> pdf("graph.pdf", height=5, width=15)
  
> barplot(as.matrix(t(data_p[,6:9])), names.arg=data_p$V2, col=rainbow(4), border= rainbow(4), las=2, cex.names=0.35, cex.axis =0.65)
  

  
> dev.off()
  
null device
  
          1
  
>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-552227-1-1.html 上篇帖子: python开发环境搭建---pyenv安装python3.5.2 下篇帖子: python ConfigParser 模块
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表