Import table in R without names (skip line did not work)

Asked

Viewed 145 times

0

I’m trying to import some data into R, but no matter what I do, there’s always a row with column names (row information I want to skip). This is the table:

Quero importar as informações a partir da linha 2

However, in R, even skipping the first line, this information comes along.

Mesmo pulando a linha 1, as infoirmações em destaque são importadas

Follows code for reproducibility of the problem:

library(tidyverse)
dados <- tribble(
  ~Regiao, ~`Obitos fetais`, ~X1, ~X2, ~X3, ~X4, ~Obitos, ~X5, ~X6, ~X7, ~X8,
  NA, 2005, 2006, 2007, 2008, 2009,  2005, 2006, 2007, 2008, 2009,
  "Norte", 2106, 2159, 2038, 2119, 2099, 52815, 52286, 53538, 5507, 57375,
  "Nordeste", 6896, 6776, 6479, 6941, 6947, 242303, 248049, 258049, 263198, 267177,
  "sudeste", 12356, 11749, 11213, 10962, 10632, 478126, 493358, 493478, 501578, 511492,
  "Sul", 3605, 3462, 3251, 3221, 3024, 159760, 163914, 169124, 169337, 174871,
  "Centro-Oeste", 1856, 1922, 1781, 1725, 1710, 60573, 62585, 64353, 66641, 68301
)

library(openxlsx)
write.xlsx(dados, "meusdados.xlsx", sheetName = "tipo4")

tipo4 <- xlsx::read.xlsx("meusdados.xlsx", "tipo4", starRow = 2, endRow = 7)
  • 1

    seems to me a typo: the argument name is startRow and you used starRow.

  • 3

    @Maniero I would only change the reason for closing p/ "This problem cannot be reproduced, or is a typo. Even within the scope of the site, your solution would hardly be useful to other users in the future. Problems like this can be avoided by creating a Minimum, Complete and Verifiable example."

  • 2

    @Danielfalbel done!

  • Thank you very much!

1 answer

2

In a sentence

As indicated in the comments, this is a typo and it can be corrected by changing the argument starRow (without the t) for startRow.

tipo4 <- xlsx::read.xlsx("meusdados.xlsx", "tipo4", starRow = 2, endRow = 7)
tipo4
        Regiao Obitos.fetais    X1    X2    X3    X4 Obitos     X5     X6     X7     X8 starRow
1         <NA>          2005  2006  2007  2008  2009   2005   2006   2007   2008   2009       2
2        Norte          2106  2159  2038  2119  2099  52815  52286  53538   5507  57375       2
3     Nordeste          6896  6776  6479  6941  6947 242303 248049 258049 263198 267177       2
4      sudeste         12356 11749 11213 10962 10632 478126 493358 493478 501578 511492       2
5          Sul          3605  3462  3251  3221  3024 159760 163914 169124 169337 174871       2
6 Centro-Oeste          1856  1922  1781  1725  1710  60573  62585  64353  66641  68301       2

and once corrected,

tipo4 <- xlsx::read.xlsx("meusdados.xlsx", "tipo4", startRow = 2, endRow = 7)
tipo4
           NA. X2005 X2006 X2007 X2008 X2009 X2005.1 X2006.1 X2007.1 X2008.1 X2009.1
1        Norte  2106  2159  2038  2119  2099   52815   52286   53538    5507   57375
2     Nordeste  6896  6776  6479  6941  6947  242303  248049  258049  263198  267177
3      sudeste 12356 11749 11213 10962 10632  478126  493358  493478  501578  511492
4          Sul  3605  3462  3251  3221  3024  159760  163914  169124  169337  174871
5 Centro-Oeste  1856  1922  1781  1725  1710   60573   62585   64353   66641   68301

Learning from the mistake

The mistake happened silently and it makes it difficult to find and understand it.

The did it because he thought the argument starRow = 2 was the command to include a column with that name (look at the last column of the first case and even the screenshot of the question).

So when something turns out different than expected, looking at the result calmly can help you understand the origin of the problem and how to solve it.

In addition, the use of autocomplete offered by Ides (as the RStudio), can help prevent problems like this. Here has information about the RStudio.

  • Thank you so much for finding the error and for your "Learning from Error" tip. It helped me a lot! valeu!

  • The error was found by @Daniel, I only put as answer :P

  • Ops. Thank you very much! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.