How to computate the gini index by column when NA is there.

Hi,
how to compute the gini index use Desctools or other packages in R? I could only complete one column that not have na. Take the “col1”,“col2”, “col3” for example. Thank you in advance.

c1<-c(16.6,1.0,10.1,8.6,8.0,17.0,2.4,7.6,5.7,11.6,3.6,2.8,6.3,1.5,2.7,7.4,6.2,2.5,6.0,5.5,2.0,6.7,5.6,3.5,10.4,6.8,3.5,NA,NA)
c2<-c(9.3,10.6,1.3,2.8,5.8,NA,9.4,2.5,3.2,7.3,12.5,18.4,15.0,11.8,13.7,10.2,9.5,7.6,2.5,13.6,4.8,3.6,3.2,10.2,17.7,6.8,3.5,18.9,NA)
c3<-c(5.6,5.8,13.3,13.8,3.0,10.1,10.7,7.7,12.3,15.8,3.5,12.7,4.2,8.1,13.6,19.8,14.3,16.8,9.9,6.6,8.9,10.8,13.1,12.1,5.9,10.1,11.2,18.8,11.0)
c4<-c(1:29)
c5<-data.frame(c4,c1,c3,c2)
colnames(c5)<-c("id","col1","col2","col3")

Created on 2022-10-31 with reprex v2.0.2

1 Like

The function signature, from help(Gini)

Gini(x, weights = NULL, unbiased = TRUE,
     conf.level = NA, R = 10000, type = "bca", na.rm = FALSE)

so, set

na.rm = TRUE

OK, as we could see, there are 3 columns in c5 dataframe. Is it possible to calculate gini index for each column using loop function, apply function or others? when we calculate gini index for col1, ignoring the NA. Others are the same. Thanks a lot.

c1 <- c(16.6, 1.0, 10.1, 8.6, 8.0, 17.0, 2.4, 7.6, 5.7, 11.6, 3.6, 2.8, 6.3, 1.5, 2.7, 7.4, 6.2, 2.5, 6.0, 5.5, 2.0, 6.7, 5.6, 3.5, 10.4, 6.8, 3.5, NA, NA)
c2 <- c(9.3, 10.6, 1.3, 2.8, 5.8, NA, 9.4, 2.5, 3.2, 7.3, 12.5, 18.4, 15.0, 11.8, 13.7, 10.2, 9.5, 7.6, 2.5, 13.6, 4.8, 3.6, 3.2, 10.2, 17.7, 6.8, 3.5, 18.9, NA)
c3 <- c(5.6, 5.8, 13.3, 13.8, 3.0, 10.1, 10.7, 7.7, 12.3, 15.8, 3.5, 12.7, 4.2, 8.1, 13.6, 19.8, 14.3, 16.8, 9.9, 6.6, 8.9, 10.8, 13.1, 12.1, 5.9, 10.1, 11.2, 18.8, 11.0)

the_series <- list(c1,c2,c3)
my_gini <- function(x) DescTools::Gini(x, na.rm = TRUE)
the_ginis <- lapply(the_series,my_gini)
the_ginis |> unlist()
#> [1] 0.3537744 0.3464965 0.2344565

Created on 2022-11-08 by the reprex package (v2.0.1)

1 Like

It works!!! I am very grateful to your effort and time in this matter.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.