Loop through all data frames in Environment a compute GINI

Hi, I have a following problem. I would like to loop through all data frames I have in my environment. Then I would like to compute Gini index for each. I tried this:

library(DescTools)

dfs <- ls()[sapply(mget(ls(), .GlobalEnv), is.data.frame)]

for (j in 1:length(dfs)){
  Gini(dfs[j]$value)
}

But I got an error: Error: $ operator is invalid for atomic vectors. How can I fix it please? Thanks a lot!

What happens if you try [[j]]? If this doesn't work I suggest posting some data so we can help (see here: FAQ: How to do a minimal reproducible example ( reprex ) for beginners)

library(DescTools)

dfs <- ls()[sapply(mget(ls(), .GlobalEnv), is.data.frame)]

for (j in 1:length(dfs)){
  Gini(dfs[[j]]$value)
}

Something like the below?

library(DescTools)

df1 <- data.frame(name = c("a", "a", "b", "b", "c", "d"), value = c(5, 6, 15, 20, 35, 150), 
                  year = c("2000","2000","2000","2001","2000","2000" ))

df2 <- data.frame(name = c("a", "b", "b", "b", "c", "d"), value = c(51, 26, 111, 24, 35, 150), 
                  year = c("2000","2000","2000","2001","2000","2000" ))


#dfs <- ls()[sapply(mget(ls(), .GlobalEnv), is.data.frame)]
#dfs <- eapply(.GlobalEnv,get())

dfs <- sapply(.GlobalEnv, is.data.frame) 

dfx <- list()

for (i in 1:length(dfs)) {
  dfx[i] <- mget(names(dfs)[i])
}


for (j in 1:length(dfx)){
  Gini(dfx[[j]]$value)
  print(Gini(dfx[[j]]$value))
}
#> [1] 0.7073593
#> [1] 0.4539043

Created on 2020-11-09 by the reprex package (v0.3.0)

This topic was automatically closed 21 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.

Gini(dfs[[j]]$value) leads to the same error: Error: $ operator is invalid for atomic vectors.

Reprex:

library(DescTools)

df1 <- data.frame(name = c("a", "a", "b", "b", "c", "d"), value = c(5, 6, 15, 20, 35, 150), 
                 year = c("2000","2000","2000","2001","2000","2000" ))

df2 <- data.frame(name = c("a", "b", "b", "b", "c", "d"), value = c(51, 26, 111, 24, 35, 150), 
                 year = c("2000","2000","2000","2001","2000","2000" ))


dfs <- ls()[sapply(mget(ls(), .GlobalEnv), is.data.frame)]

for (j in 1:length(dfs)){
  Gini(dfs[[j]]$value)
}

Hello,

What you provided is not a reprex as Gini is not defined

df1 <- data.frame(name = c("a", "a", "b", "b", "c", "d"), value = c(5, 6, 15, 20, 35, 150), 
                  year = c("2000","2000","2000","2001","2000","2000" ))

df2 <- data.frame(name = c("a", "b", "b", "b", "c", "d"), value = c(51, 26, 111, 24, 35, 150), 
                  year = c("2000","2000","2000","2001","2000","2000" ))


#dfs <- ls()[sapply(mget(ls(), .GlobalEnv), is.data.frame)]

dfs <- list(df1,
            df2)


for (j in 1:length(dfs)){
  print(dfs[[j]]$value)
}
#> [1]   5   6  15  20  35 150
#> [1]  51  26 111  24  35 150

Created on 2020-11-09 by the reprex package (v0.3.0)

You'll see below I altered the code just to show you the format works now. The issue is your dfs structure is not a list but a character vector. You will see that it runs when you have a true list object passed into it.

I shared it in this way to show you the dfs[[j]]$value convention works and the issue is in fact your character vector setup.

Thanks, I forgot to include library(DescTools), sorry for that. Your code:

dfs <- list(df1,
            df2)

is not applicable in my case, because I have a lot of data frames in my environment and I cannot manually include them into the list().

OK and do you have an idea, how can I fix it, please?