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

0

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

However,inR,evenbyjumpingthefirstline,thisinformationcomestogether.

Hereisthecodeforproblemreproducibility:

library(tidyverse)dados<-tribble(~Regiao,~'Obitosfetais',~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)
    
asked by anonymous 18.12.2018 / 14:16

1 answer

2

In a sentence

As indicated in the comments, this is a typo and can be fixed by changing the argument starRow (without t ) to 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 fixed,

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 error

The error happened silently and this makes it difficult to find and understand it.

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

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

In addition, using autocomplete offered by IDEs (such as RStudio ) can help prevent such problems. Here has information about autocomplete in RStudio .

    
20.12.2018 / 19:16