zzl001 发表于 2018-8-15 12:38:48

python查找指定数据,用R绘图

> 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)))
  
    }
  
    .Internal(mean(x))
  
}
  
<bytecode: 0x00000000111ee3c0>
  
<environment: namespace:base>
  

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

  

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

  
> mt(1:10)
  
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 46689
  
7   7 46614
  
8   83456
  
9   9   5   7
  
10 10   2   2
  

  
> apply(data1, 2, mt)
  
      a    b   c
  
1    2   2
  
2    5   5
  
3    6   6
  
4    7   7
  
5466 466
  
6 -472 -44
  
746614
  
8   3456
  
9    5   7
  
5    2   2
  

  
> data1 <- data.frame(rbind(a,b,c) )
  
> data1
  
X1 X2 X3 X4X5X6X7 X8 X9 X10
  
a1234   5   6   78910
  
b2567 466 466 466 345   2
  
c2567 4668914 567   2
  

  
> apply(data1, 1, mt)
  
    a    b   c
  
X11    2   2
  
X22    5   5
  
X33    6   6
  
X44    7   7
  
X55466 466
  
X66 -472 -44
  
X7746614
  
X88   3456
  
X99    5   7
  
X10 5    2   2
  

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

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

  
> c("A","B",3)
  
"A" "B" "3"
  

  
> mt <- function(v){
  
       n <- length(v)
  
       t1 <- v
  
       t2 <- as.numeric(v)
  
       tp <- sum(t2)
  

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

  
>
  
> apply(data, 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    V4V5
  
"1" "100" "(0)" "1" ":" "0.663"            "0.185"
  
"2" "101" "(0)" "1" ":" "0.767"            "0.042"
  
"3" "102" "(0)" "1" ":" "0.651"            "0.083"
  
"4" "103" "(0)" "1" ":" "0.358"            "0.078"
  
"5" "104" "(0)" "1" ":" "0.062"            "0.096"
  
"6" "105" "(0)" "1" ":" "0.058"            "0.098"
  

  

  

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

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

  

  
> head(data)
  
V1V2V3 V4 V5    V6    V7    V8    V9
  
11 100 (0)1: 0.663 0.185 0.038 0.113
  
22 101 (0)1: 0.767 0.042 0.033 0.158
  
33 102 (0)1: 0.651 0.083 0.062 0.204
  
44 103 (0)1: 0.358 0.078 0.185 0.379
  
55 104 (0)1: 0.062 0.096 0.487 0.355
  
66 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    V4V5
  
"1" "100" "(0)" "1" ":" "0.663" "0.185" "0.039" "0.113"
  
"2" "101" "(0)" "1" ":" "0.767" "0.042" "0.033" "0.158"
  
"3" "102" "(0)" "1" ":" "0.651" "0.083" "0.062" "0.204"
  
"4" "103" "(0)" "1" ":" "0.358" "0.078" "0.185" "0.379"
  
"5" "104" "(0)" "1" ":" "0.062" "0.096" "0.487" "0.355"
  
"6" "105" "(0)" "1" ":" "0.058" "0.098" "0.515" "0.329"
  

  
> apply(data[,6:9], 1 , sum)
  
0.999 1.000 1.000 1.000 1.000 1.001 1.000 1.000 1.000 1.000 1.000 1.000
  
1.001 1.000 1.000 1.001 0.999 0.999 1.000 1.000 1.000 1.000 1.000 1.000
  
1.001 1.000 1.001 1.000 0.999 1.000 1.000 1.000 1.000 1.000 1.001 1.000
  
1.000 1.000 0.999 1.000 0.999 1.001 1.000 0.999 1.000 1.000 1.000 1.000
  
1.000 0.999 1.000 1.000 1.001 1.000 1.001 1.000 1.000 1.001 1.000 1.000
  
1.000 1.000 1.000 1.001 0.999 1.000 0.999 1.001 0.999 0.999 1.000 1.000
  
0.999 1.000 1.000 1.000 0.999 1.000 1.000 1.001 1.000 1.000 1.000 1.001
  
1.000 1.001 1.000 1.000 1.000 1.001 1.000 1.000 1.000 1.000 1.000 1.001
  
1.001 1.000 1.000 1.000 1.000 1.000 1.001 1.001 1.000 1.001 1.000 1.000
  
1.000 1.000 0.999 0.999 1.001 1.000 1.000 1.000 0.999 1.001 1.000 1.000
  
1.000 1.000 1.000 1.000 1.000 0.999 1.000 1.000 1.000 1.000 1.000 1.000
  
1.000 1.001 1.001 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000
  
1.001 0.999 1.000 0.999 1.000 1.000 0.999 1.000 0.999 1.000 0.999 1.000
  
0.999 0.999 1.000 1.001 1.000 1.001 1.000 1.000 1.000 0.999 1.000 1.001
  
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    V4V5
  
"1" "100" "(0)" "1" ":" "0.663" "0.185" "0.039" "0.113"
  
"2" "101" "(0)" "1" ":" "0.767" "0.042" "0.033" "0.158"
  
"3" "102" "(0)" "1" ":" "0.651" "0.083" "0.062" "0.204"
  
"4" "103" "(0)" "1" ":" "0.358" "0.078" "0.185" "0.379"
  
"5" "104" "(0)" "1" ":" "0.062" "0.096" "0.487" "0.355"
  
"6" "105" "(0)" "1" ":" "0.058" "0.098" "0.515" "0.329"
  

  

  
> head(x[,6:9])
  

  
"0.663" "0.185" "0.039" "0.113"
  
"0.767" "0.042" "0.033" "0.158"
  
"0.651" "0.083" "0.062" "0.204"
  
"0.358" "0.078" "0.185" "0.379"
  
"0.062" "0.096" "0.487" "0.355"
  
"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 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 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 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 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 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 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 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 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    V4V5
  
"1" "100" "(0)" "1" ":" "0.663" "0.185" "0.039" "0.113"
  
"2" "101" "(0)" "1" ":" "0.767" "0.042" "0.033" "0.158"
  
"3" "102" "(0)" "1" ":" "0.651" "0.083" "0.062" "0.204"
  
"4" "103" "(0)" "1" ":" "0.358" "0.078" "0.185" "0.379"
  
"5" "104" "(0)" "1" ":" "0.062" "0.096" "0.487" "0.355"
  
"6" "105" "(0)" "1" ":" "0.058" "0.098" "0.515" "0.329"
  

  
> x1 <- as.data.frame(x)
  
> head(x1)
  
   V1V2V3 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, 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]
查看完整版本: python查找指定数据,用R绘图