DF as list: non numeric argument to binary operator data frame r error

Hi everyone, I'm freshly using R and I have one problem.

I've some huge data frames and I need to do some operations with it, but one df is defined as list, as can be read bellow:

The comparable dfs received the same treatments and are ok, only this one is with this problem.

So, when I'm trying to do the operations, I receive this message:

non numeric argument to binary operator data frame r

But my data frame is numeric!

I just tried to define as.data.frame and as.numeric but without success.

Anyone now what to do?

Hi there

It's very hard to tell what the problem is from the screenshot -- but it is a common error when I work! It would be helpful if you could post a piece of the list that is causing the problem. You can subset a list with square brackets, so

dput(your_list_name[1:10])

would make a text version of the first 10 items in your list. Then post the code that you are applying to that list that is causing the error.

This FAQ provides more information on making a reproducible example

Hi @atiretoo, thank u for your answer.

This code give me this:

> dput(DadosRGF.UF_2.191q_2[1:10])
structure(list(Conta = c("DÍVIDA CONSOLIDADA - DC (I)", "Dívida Mobiliária", 
"Dívida Contratual", "Interna", "Empréstimos Internos", "Reestruturação da Dívida de Estados e Municípios", 
"Financiamentos Internos", "Demais Dívidas Contratuais", "Externa", 
"Empréstimos Externos", "Financiamentos Externos", "Precatórios Posteriores a 05/05/2000 (inclusive) Vencidos e Não Pagos", 
"Outras Dívidas", "DEDUÇÕES (II)", "Disponibilidade de Caixa", 
"Disponibilidade de Caixa Bruta", "(-) Restos a Pagar Processados", 
"Demais Haveres Financeiros", "DÍVIDA CONSOLIDADA LÍQUIDA (DCL) (III) = (I - II)", 
"RECEITA CORRENTE LÍQUIDA - RCL (IV)", "(-) Transferências Obrigatórias da União Relativas às Emendas Individuais (art. 166-A, § 1º, da CF) (V)", 
"= RECEITA CORRENTE LÍQUIDA AJUSTADA PARA PARA CÁLCULO DOS LIMITES DE ENDIVIDAMENTO (VI) = (IV - V)", 
"% da DC sobre a RCL AJUSTADA (I/VI)", "% da DCL sobre a RCL AJUSTADA (III/VI)", 
"LIMITE DEFINIDO POR RESOLUÇÃO DO SENADO FEDERAL", "LIMITE DE ALERTA (inciso III do § 1º do art. 59 da LRF)", 
"Outros valores não integrantes da DC", "Precatórios Anteriores a 05/05/2000", 
"Precatórios Posteriores a 05/05/2000 (Não incluídos na DC)", 
"Passivo Atuarial", "Insuficiência Financeira", "Depósitos e Consignações Sem Contrapartida", 
"RP Não-Processados", "Antecipações de Receita Orçamentária - ARO", 
"Dívida Contratual de PPP", "Apropriação de Depósitos Judiciais"
), MA = list(8378348793.46854, NULL, 6957147685.34988, NULL, 
    3583867282.7532, 1543023240.55784, NULL, NULL, NULL, 1830257162.03884, 
    NULL, 1421201108.11866, NULL, 2449016552.27464, 2437006526.59543, 
    3006688577.96947, 569682051.374033, 12010025.6792103, 5929332241.1939, 
    NULL, NULL, NULL, NULL, NULL, 29352185129.6702, 26416966616.7009, 
    NULL, NULL, 186251899.567986, NULL, NULL, 533994387.231909, 
    265443846.745087, NULL, NULL, NULL), RO = list(4810857651.08788, 
    NULL, 3435481087.83705, NULL, 471087338.914385, 2802469043.4117, 
    NULL, NULL, NULL, 24515356.6834482, NULL, 1375376563.25084, 
    NULL, 2206917572.19436, 2185185822.89229, 2234968479.99081, 
    49782657.0985175, 21731749.3020685, 2603940078.89352, NULL, 
    NULL, NULL, NULL, NULL, 15718540986.3987, 14146686887.7545, 
    NULL, NULL, 372263845.071868, 2263310214.34534, NULL, NULL, 
    199158802.210013, NULL, NULL, NULL), PB = list(4907792225.04597, 
    NULL, 2780276506.52578, NULL, 299646910.984024, 1643757201.74074, 
    470706593.951642, NULL, NULL, 366165799.849376, NULL, 2127515718.52019, 
    NULL, 2345512450.9418, 2066608703.73158, 2190894205.24332, 
    124285501.511737, 278903747.210219, 2562279774.10417, NULL, 
    NULL, NULL, NULL, NULL, 21663528434.5899, 19497175591.1265, 
    NULL, NULL, NULL, 172048984.351301, NULL, 377785433.982247, 
    94606463.97021, NULL, NULL, NULL), ES = list(7599704917.19118, 
    NULL, 7483946928.23773, NULL, 3282692986.1052, 2532529446.24183, 
    NULL, NULL, NULL, 1266995768.93937, NULL, NULL, 115757988.953445, 
    6581343421.41192, 4857034559.90232, 4958359504.14668, 101324944.244357, 
    1724308861.5096, 1018361495.77926, NULL, NULL, NULL, NULL, 
    NULL, 31023258256.5359, 27920932430.8801, NULL, NULL, 183089524.267209, 
    2740481139.83209, NULL, 563479051.968486, 295094513.066483, 
    NULL, NULL, NULL), DF = list(9968190700.35933, NULL, 5029658032.92259, 
    NULL, 4022992725.19196, NULL, NULL, NULL, NULL, 965823364.842869, 
    NULL, 4938532667.43674, NULL, 2707747533.84201, 2048785456.31067, 
    2215786465.29737, 167001008.986702, 658962077.531348, 7260443166.51731, 
    NULL, NULL, NULL, NULL, NULL, 48192443175.7991, 43373198858.2214, 
    NULL, 1246987310.81148, NULL, 62722963542.0971, NULL, NULL, 
    307962352.998406, NULL, NULL, NULL), PA = list(4452698170.47648, 
    NULL, 4452698170.47648, NULL, 1974149678.25719, 1283095686.12305, 
    12267897.5678779, NULL, NULL, 1002678271.98785, NULL, NULL, 
    NULL, 2892140686.46159, 2776944329.0571, 2989734851.13896, 
    212790522.081866, 115196357.404489, 1560557484.0149, NULL, 
    NULL, NULL, NULL, NULL, 41790411860.0474, 37611370674.0471, 
    NULL, NULL, 139085859.650903, 1230969719.41993, NULL, 212782301.711919, 
    12738801.5583983, NULL, NULL, NULL), BA = list(27406249212.8599, 
    NULL, 22287856586.7843, NULL, 153095.583772151, 5722425386.12411, 
    5467175442.50601, NULL, NULL, NULL, 10645071032.5884, 4978414511.96315, 
    139978114.11238, 6541288554.31923, 5760219817.91421, 5808171744.30873, 
    47951926.3945173, 781068736.405014, 20864960658.5406, NULL, 
    NULL, NULL, NULL, NULL, 72597719402.4437, 65337947462.2016, 
    NULL, NULL, NULL, 294305697905.993, NULL, 1193102786.13454, 
    172393984.78638, NULL, 589632197.558184, NULL), MS = list(
    10085347529.9543, NULL, 9318638946.58332, NULL, 833583645.385599, 
    6915479875.04551, NULL, NULL, NULL, 1397325187.88506, NULL, 
    766708583.37095, NULL, 1935816740.73904, 1656099683.76715, 
    1811586956.92489, 155487273.157733, 279717056.971884, 8149530789.21523, 
    NULL, NULL, NULL, NULL, NULL, 23748516071.6597, 21373664464.4937, 
    NULL, NULL, NULL, 24329406035.7542, NULL, 505859654.547907, 
    231620825.312476, NULL, NULL, NULL), SC = list(26573958163.5987, 
    0, 22965996029.2197, NULL, 18462106960.6719, 0, 13812382.3924949, 
    0, NULL, 3841759305.81244, NULL, 3028599255.94463, 579362878.434315, 
    4634744542.24518, 4277070222.0657, 4663679947.33761, 386609725.27191, 
    357674320.179485, 21939213621.3535, NULL, NULL, NULL, NULL, 
    NULL, 51982335192.3887, 46784101673.1476, NULL, 10396689.440252, 
    NULL, 167690335290.897, 0, 7053811853.60019, 5825502.1182527, 
    0, 0, NULL)), row.names = c(NA, -36L), class = c("tbl_df", 
"tbl", "data.frame"))
> View(dput(DadosRGF.UF_2.191q_2[1:10]))
structure(list(Conta = c("DÍVIDA CONSOLIDADA - DC (I)", "Dívida Mobiliária", 
"Dívida Contratual", "Interna", "Empréstimos Internos", "Reestruturação da Dívida de Estados e Municípios", 
"Financiamentos Internos", "Demais Dívidas Contratuais", "Externa", 
"Empréstimos Externos", "Financiamentos Externos", "Precatórios Posteriores a 05/05/2000 (inclusive) Vencidos e Não Pagos", 
"Outras Dívidas", "DEDUÇÕES (II)", "Disponibilidade de Caixa", 
"Disponibilidade de Caixa Bruta", "(-) Restos a Pagar Processados", 
"Demais Haveres Financeiros", "DÍVIDA CONSOLIDADA LÍQUIDA (DCL) (III) = (I - II)", 
"RECEITA CORRENTE LÍQUIDA - RCL (IV)", "(-) Transferências Obrigatórias da União Relativas às Emendas Individuais (art. 166-A, § 1º, da CF) (V)", 
"= RECEITA CORRENTE LÍQUIDA AJUSTADA PARA PARA CÁLCULO DOS LIMITES DE ENDIVIDAMENTO (VI) = (IV - V)", 
"% da DC sobre a RCL AJUSTADA (I/VI)", "% da DCL sobre a RCL AJUSTADA (III/VI)", 
"LIMITE DEFINIDO POR RESOLUÇÃO DO SENADO FEDERAL", "LIMITE DE ALERTA (inciso III do § 1º do art. 59 da LRF)", 
"Outros valores não integrantes da DC", "Precatórios Anteriores a 05/05/2000", 
"Precatórios Posteriores a 05/05/2000 (Não incluídos na DC)", 
"Passivo Atuarial", "Insuficiência Financeira", "Depósitos e Consignações Sem Contrapartida", 
"RP Não-Processados", "Antecipações de Receita Orçamentária - ARO", 
"Dívida Contratual de PPP", "Apropriação de Depósitos Judiciais"
), MA = list(8378348793.46854, NULL, 6957147685.34988, NULL, 
    3583867282.7532, 1543023240.55784, NULL, NULL, NULL, 1830257162.03884, 
    NULL, 1421201108.11866, NULL, 2449016552.27464, 2437006526.59543, 
    3006688577.96947, 569682051.374033, 12010025.6792103, 5929332241.1939, 
    NULL, NULL, NULL, NULL, NULL, 29352185129.6702, 26416966616.7009, 
    NULL, NULL, 186251899.567986, NULL, NULL, 533994387.231909, 
    265443846.745087, NULL, NULL, NULL), RO = list(4810857651.08788, 
    NULL, 3435481087.83705, NULL, 471087338.914385, 2802469043.4117, 
    NULL, NULL, NULL, 24515356.6834482, NULL, 1375376563.25084, 
    NULL, 2206917572.19436, 2185185822.89229, 2234968479.99081, 
    49782657.0985175, 21731749.3020685, 2603940078.89352, NULL, 
    NULL, NULL, NULL, NULL, 15718540986.3987, 14146686887.7545, 
    NULL, NULL, 372263845.071868, 2263310214.34534, NULL, NULL, 
    199158802.210013, NULL, NULL, NULL), PB = list(4907792225.04597, 
    NULL, 2780276506.52578, NULL, 299646910.984024, 1643757201.74074, 
    470706593.951642, NULL, NULL, 366165799.849376, NULL, 2127515718.52019, 
    NULL, 2345512450.9418, 2066608703.73158, 2190894205.24332, 
    124285501.511737, 278903747.210219, 2562279774.10417, NULL, 
    NULL, NULL, NULL, NULL, 21663528434.5899, 19497175591.1265, 
    NULL, NULL, NULL, 172048984.351301, NULL, 377785433.982247, 
    94606463.97021, NULL, NULL, NULL), ES = list(7599704917.19118, 
    NULL, 7483946928.23773, NULL, 3282692986.1052, 2532529446.24183, 
    NULL, NULL, NULL, 1266995768.93937, NULL, NULL, 115757988.953445, 
    6581343421.41192, 4857034559.90232, 4958359504.14668, 101324944.244357, 
    1724308861.5096, 1018361495.77926, NULL, NULL, NULL, NULL, 
    NULL, 31023258256.5359, 27920932430.8801, NULL, NULL, 183089524.267209, 
    2740481139.83209, NULL, 563479051.968486, 295094513.066483, 
    NULL, NULL, NULL), DF = list(9968190700.35933, NULL, 5029658032.92259, 
    NULL, 4022992725.19196, NULL, NULL, NULL, NULL, 965823364.842869, 
    NULL, 4938532667.43674, NULL, 2707747533.84201, 2048785456.31067, 
    2215786465.29737, 167001008.986702, 658962077.531348, 7260443166.51731, 
    NULL, NULL, NULL, NULL, NULL, 48192443175.7991, 43373198858.2214, 
    NULL, 1246987310.81148, NULL, 62722963542.0971, NULL, NULL, 
    307962352.998406, NULL, NULL, NULL), PA = list(4452698170.47648, 
    NULL, 4452698170.47648, NULL, 1974149678.25719, 1283095686.12305, 
    12267897.5678779, NULL, NULL, 1002678271.98785, NULL, NULL, 
    NULL, 2892140686.46159, 2776944329.0571, 2989734851.13896, 
    212790522.081866, 115196357.404489, 1560557484.0149, NULL, 
    NULL, NULL, NULL, NULL, 41790411860.0474, 37611370674.0471, 
    NULL, NULL, 139085859.650903, 1230969719.41993, NULL, 212782301.711919, 
    12738801.5583983, NULL, NULL, NULL), BA = list(27406249212.8599, 
    NULL, 22287856586.7843, NULL, 153095.583772151, 5722425386.12411, 
    5467175442.50601, NULL, NULL, NULL, 10645071032.5884, 4978414511.96315, 
    139978114.11238, 6541288554.31923, 5760219817.91421, 5808171744.30873, 
    47951926.3945173, 781068736.405014, 20864960658.5406, NULL, 
    NULL, NULL, NULL, NULL, 72597719402.4437, 65337947462.2016, 
    NULL, NULL, NULL, 294305697905.993, NULL, 1193102786.13454, 
    172393984.78638, NULL, 589632197.558184, NULL), MS = list(
    10085347529.9543, NULL, 9318638946.58332, NULL, 833583645.385599, 
    6915479875.04551, NULL, NULL, NULL, 1397325187.88506, NULL, 
    766708583.37095, NULL, 1935816740.73904, 1656099683.76715, 
    1811586956.92489, 155487273.157733, 279717056.971884, 8149530789.21523, 
    NULL, NULL, NULL, NULL, NULL, 23748516071.6597, 21373664464.4937, 
    NULL, NULL, NULL, 24329406035.7542, NULL, 505859654.547907, 
    231620825.312476, NULL, NULL, NULL), SC = list(26573958163.5987, 
    0, 22965996029.2197, NULL, 18462106960.6719, 0, 13812382.3924949, 
    0, NULL, 3841759305.81244, NULL, 3028599255.94463, 579362878.434315, 
    4634744542.24518, 4277070222.0657, 4663679947.33761, 386609725.27191, 
    357674320.179485, 21939213621.3535, NULL, NULL, NULL, NULL, 
    NULL, 51982335192.3887, 46784101673.1476, NULL, 10396689.440252, 
    NULL, 167690335290.897, 0, 7053811853.60019, 5825502.1182527, 
    0, 0, NULL)), row.names = c(NA, -36L), class = c("tbl_df", 
"tbl", "data.frame"))

and looks like:

For the calculus I'm only using from [2:28], without the text column.

I tried to upload the xls file but the site didin't allowed me to.

Is this of any help?

Thanks for the data sample, that is helpful.

library(tidyverse)
#df <- structure(list(Conta = c("DÍVIDA CONSOLIDADA - DC (I)", "Dívida Mobiliária", 
 #                        "Dívida Contratual", "Interna", "Empréstimos Internos", "Reestruturação da Dívida de Estados e Municípios", 
# <data deleted to save space 
map_chr(df, class)
#>       Conta          MA          RO          PB          ES          DF 
#> "character"      "list"      "list"      "list"      "list"      "list" 
#>          PA          BA          MS          SC 
#>      "list"      "list"      "list"      "list"

Somehow each of the columns has been read into a list,
which even though it has components with numeric values isn’t
itself numeric. You should compare the structure of the
dataframe with the ones that work, and try to figure out
why it’s coming out this way. You mentioned adding rows to
the dataframe to make it longer, check the structure before
and after that step in particular. As a bandage, we can
convert the lists back to numeric vectors.
So start by extracting one column and try converting it

unlist(df[[2]]) # only pulls out the non-null ones
#>  [1]  8378348793  6957147685  3583867283  1543023241  1830257162  1421201108
#>  [7]  2449016552  2437006527  3006688578   569682051    12010026  5929332241
#> [13] 29352185130 26416966617   186251900   533994387   265443847

We have to deal with the NULL values. You tried converting the nulls to 0.

df[[2]][is.null(df[[2]])] <- 0 
df[[2]]
#> [[1]]
#> [1] 8378348793
#> 
#> [[2]]
#> NULL
#> 
#> [[3]]
#> [1] 6957147685
#> 
#> [[4]]
#> NULL
# ... stuff deleted to save space

But this doesn’t do anything. Digging in to the logical test

is.null(df[[2]]) 
#> [1] FALSE

you can see this only tests first layer of nested list, and returns a single FALSE.

If we map over the list-column we get the desired result.

pick <- map_lgl(df[[2]], is.null)
df[[2]][pick] <- 0
df[[2]] 
#> [[1]]
#> [1] 8378348793
#> 
#> [[2]]
#> [1] 0
#> 
#> [[3]]
#> [1] 6957147685
#> 
#> [[4]]
#> [1] 0
#> 
# stuff deleted ... 

That changes the NULL values to 0, but doesn’t unnest the list

map() and friends can extract values from a list component by position.
In this case we have a list of lists of length 1, so we just pull out the
first element.

map_dbl(df[[3]], 1) # the 1 pulls out the first element in the list
#> Error: Result 2 must be a single double, not NULL of length 0

That causes an error, because the second element is a NULL, which is
not a list. The argument .default will fill in an error with whatever
value we want.

map_dbl(df[[3]], 1, .default = NA_real_)
#>  [1]  4810857651          NA  3435481088          NA   471087339  2802469043
#>  [7]          NA          NA          NA    24515357          NA  1375376563
#> [13]          NA  2206917572  2185185823  2234968480    49782657    21731749
#> [19]  2603940079          NA          NA          NA          NA          NA
#> [25] 15718540986 14146686888          NA          NA   372263845  2263310214
#> [31]          NA          NA   199158802          NA          NA          NA

I think NULL should be missing, but if you prefer a zero just
change NA_real_ to 0. Now do that for every column:

df2 <- map_dfc(df[2:10], ~map_dbl(.x, 1, .default = NA_real_))
df2 <- bind_cols(df[,1],df2)
map_chr(df2, class)
#>       Conta          MA          RO          PB          ES          DF 
#> "character"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric" 
#>          PA          BA          MS          SC 
#>   "numeric"   "numeric"   "numeric"   "numeric"

And that’s got the columns converted to numeric.

Without seeing the other data structures, and the code that you are using to manipulate them, I can't help you further. Hope that's helpful.

Created on 2021-06-15 by the reprex package (v2.0.0)

The difference between the dfs are that the most recent have two more rows with data, and I already included these rows with 0 to not have problems.

And this didn't work, I also tried to convert to numeric with two other codes:

#DadosRGF.UF_2.19_2[, c(1:28)] <- sapply(DadosRGF.UF_2.19_2[, c(1:28)], as.numeric)

and:

DadosRGF.UF_2.191q_2 <- as.numeric(as.character(unlist(DadosRGF.UF_2.191q_2[[1]])))

But not worked, I also tried to convert the NULLs to 0 to check if this can solve the problem, with:

DadosRGF.UF_2.191q_2[is.null(DadosRGF.UF_2.191q_2)] <- 0

But nothing happened. I really don't know why this df is with this problem while the other 3 have the same structure and are ok

I named the first dataset your shared the dput() of as 'df1' to save myself typing.
The following code unwraps the lists


myunlister <- function(x){
  map_dbl(df1[[x]],~ifelse(is.null(.),NA,.))
}                                              
names_to_unlist <- setdiff(names(df1),"Conta")

map_dfc(names_to_unlist, ~ enframe(x=myunlister(.),
                              name = NULL,
                              value=.))

Hey @nirgrahamuk , thank you very much!!

This code solved my problem!!!!

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.