R - Create binary variable (dummy) value 1 for 50% of the total

0

Well, I would like a help, I have this data each column has 100%, first wanted to index from highest to lowest and then make use of the dummy variable with a value of 1 for species that add up to 50%, the rest would be zero.

           Nome      Dens      Freq       Dom
1       Abarema  9.090909 46.153846 29.411765
2         Abuta 13.636364 11.538462 23.529412
3     Agonandra 18.181818  7.692308 11.764706
4        Aiouea 22.727273 15.384615 29.411765
5 Alchorneopsis 36.363636 19.230769  5.882353

Result:

           Nome      Dens      Freq       Dom v1 v2 v3
1       Abarema  9.090909 46.153846 29.411765  0  1  1
2         Abuta 13.636364 11.538462 23.529412  0  0  0
3     Agonandra 18.181818  7.692308 11.764706  0  0  0
4        Aiouea 22.727273 15.384615 29.411765  1  0  1
5 Alchorneopsis 36.363636 19.230769  5.882353  1  1  0

If you can help me, I'm grateful.

    
asked by anonymous 20.07.2018 / 07:34

1 answer

1

I think the following code solves the question problem.

First, I define a function that processes the columns of class numeric and creates each dummy . Do this by adding the values from the highest to reach or exceed 50%. These values will be encoded as 1L (class integer ) and others as 0L .

dummyFun <- function(x){
  n <- NROW(x)
  inx <- order(x, decreasing = TRUE)
  d <- which(cumsum(x[inx]) >= 50)[1]
  d <- c(rep(1L, d), rep(0L, n - d))[order(inx)]
  d
}

num <- sapply(dados, is.numeric)

dum <- sapply(dados[num], dummyFun)
colnames(dum) <- paste0("v", seq_len(ncol(dum)))
Resultado <- cbind(dados, dum)
rm(dum, num)    # Limpeza final

Resultado
#           Nome      Dens      Freq       Dom v1 v2 v3
#1       Abarema  9.090909 46.153846 29.411765  0  1  1
#2         Abuta 13.636364 11.538462 23.529412  0  0  0
#3     Agonandra 18.181818  7.692308 11.764706  0  0  0
#4        Aiouea 22.727273 15.384615 29.411765  1  0  1
#5 Alchorneopsis 36.363636 19.230769  5.882353  1  1  0
    
20.07.2018 / 20:34