Home > Software engineering >  R percentage calculation
R percentage calculation

Time:11-22

I have my code

V_max=15
V_min=0
H=1
n=1
x_right <- 3.2
x1_right <- 11.8
par(mfrow=c(length(C) 1,1), mar = c(2,0,2,0),oma = c(1,5,0,0))


fun <- function( C, H, n ){
  1 / (1   (C / H)^n) 
}

x_lim_min=-1
x_lim_max=13



C=c(0,0.01,0.1,1)

mylist <- list()
for(i in 1:length(C)){
  V_C <- V_max*fun(C[i],H,n)
  x3 <- rnorm(100,V_C,1)
  mylist[i] <- mean(x3)
  y3 <- hist(x3, plot=FALSE,breaks=20)
  maans_to_hist <- unlist(mylist)
  mylist[[i]] <- mean(x3)
  plot(y3, col='gray48',xlim=c(x_lim_min,x_lim_max))
}

I want to add calculation my loop-for the following conditions. I tried this code :

c1 <- apply(x3,2, function(V) (x3 < x1_right)) / 100
c2 <- apply(x3,2, function(V) (x3 > x_right & x3 < x1_right)) / 100

and I got error:

Error in apply(x3, 2, function(V) (x3 < x1_right)) : 
  dim(X) must have a positive length

That is, the percentage of observation in each of my histograms satisfying these conditions (x3 < x1_right)) / 100 and (x3 > x_right & x3 < x1_right) / 100. I do not know how to add it to the loop - for (what I care about), so please help

CodePudding user response:

Assuming I don't know your desired result and, as we're using random, set.seed() so everyone gets consistent results

Error in apply(x3, 1, function(V) (x3 < x1_right)) : 
  dim(X) must have a positive length
> for(i in 1:length(C)){
  browser()
  V_C <- V_max*fun(C[i],H,n)
  x3 <- rnorm(100,V_C,1)
  mylist[[i]] <- mean(x3)
  y3 <- hist(x3, plot=FALSE,breaks=20)
  c1 <- apply(x3,1, function(V) (x3 < x1_right)) / 100
  c2 <- apply(x3,1, function(V) (x3 > x_right & x3 < x1_right)) / 100
  plot(y3, col='gray48',xlim=c(x_lim_min,x_lim_max))
  mylist <- c(mylist, y3, c1, c2)
  }

There are several problems. x1_right is in all cases below x3 values, so c1 would want to be (x3 > x1_right))/100. But this still won't work with apply as it expects an object that has dimensions dim. Still within the debug:

Browse[1]> x1_right
[1] 11.8
Browse[1]> (x3 < x1_right)/100
  [1] 0 0 0 0 0 0 0 0 0 0 0...100 
Browse[1]> (x3 > x1_right)/100
  [1] 0.01 0.01 0.01 0.01 0.01...100
dim((x3 < x1_right)/100)
NULL
dim(x3) <- length(x3)
Browse[1]> dim(x3)
[1] 100
c1 <- apply(x3,1,function(V) (x3 > x1_right))/100
Browse[1]> dim(c1)
[1] 100 100
# all values `0.01`

which suggests going to lists() after constants (yours above)

V_max=15
V_min=0
H=1
n=1
x_right <- 3.2
x1_right <- 11.8
par(mfrow=c(length(C) 1,1), mar = c(2,0,2,0),oma = c(1,5,0,0))
fun <- function( C, H, n ){
  1 / (1   (C / H)^n) 
}
x_lim_min=-1
x_lim_max=13
C=c(0,0.01,0.1,1)
x3 <- list()
y3 <- list()
c1 <- list()
c2 <- list()
mean_x3 <- NULL
# these in addition to your constants above...
set.seed(42)
for(i in 1:length(C)){
browser()
V_C[i] <- V_max*fun(C[i],H,n)
x3[[i]] <- rnorm(100,V_C[i],1)
dim(x3[[i]]) <- length(x3[[i]])# or however many you're looking for
mean_x3[i] <- mean(x3[[i]])
y3[[i]] <- hist(x3[[i]], plot=FALSE,breaks=20)
c1[[i]] <- apply(x3[[i]],1, function(V) (x3[[i]] < x1_right)) / 100
c2[[i]] <- apply(x3[[i]],1, function(V) (x3[[i]] > x_right & x3[[i]] < x1_right)) / 100
plot(y3[[i]], col='gray48',xlim=c(x_lim_min,x_lim_max))
#mylist[[i]] <- list(x3[[i]], mean_x3[[i]], y3[[i]], c1[[i]], c2[[i]])
# replace with named list that makes `str(` and others useful
mylist[[i]] <- list(x3=x3[[i]], mean_x3=mean_x3[[i]], y3=y3[[i]], c1=c1[[i]], c2=c2[[1]])
}

which at least runs. Comment out # browser() if these are in any way sane results you're looking for.

  • Related