Error in scan (file=file, what=what...)

Asked

Viewed 467 times

1

Good Morning, I’m having a bug to run a code on my R.

Follow the code below.

library(car)
library(fBasics)
library(NbClust)

setwd("C:/Financas") # Salvar na pasta
Base = read.table("baseFin.txt", header=TRUE) #Importa o arquivo

head(Base)
tail(Base)

papel       = Base$Papel
evebit      = Base$EVEBIT
cresrec     = Base$CresRec
lpa         = Base$LPA
vpa         = Base$VPA
margliq     = Base$MargLiquida
ebitativo   = Base$EBITAtivo
liqucorr    = Base$LiquCorr
divbrpatr   = Base$DivBrPatrim

The main purpose of these codes is to take the indicators in the database and do what is written in the code.

This is the database

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

That’s the mistake that shows up on my R

Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,  
: 
linha 1 não tinha 14 elementos
  • In your first line, which you indicate as a header = TRUE, there is space between the words of the title of the same column. Try to put a character like _ between the words.

  • @Matheustostes, avoid sharing information as image. Take a look here on how to improve your next questions.

1 answer

1

The problem is that your header has column names with spaces. You have some options.

Changes the file. The best, but not always possible.

Ignore the header. And enter the names.

Base = read.table("baseFin.txt", skip = 1, col.names = c("col1", "col2", "col3") # se vira com os nomes porque o seu arquivo é uma imagem.

If your file is separated by tab, you can use the column names as they are. But it does not, please.

 Base = read.csv2("baseFin.txt", sep="\t", header=TRUE, check.names=FALSE)

Browser other questions tagged

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