0
I’m working with a large database and it has a few lines with blank Ids. And I want to save only lines without blank ID in an Excel file.
Whereas the Ids are in the first column of my matrix, here’s what I’m doing:
k <- which(is.na(Dados[,1]))
if (length(k) > 0) Dados <- Dados[-k,]
library(XLConnect)
wb <- loadWorkbook("Pasta\\Nome.xls")
writeWorksheet(wb,data=Dados,sheet=1,header=TRUE)
saveWorkbook(wb)
I don’t know why, but the resulting spreadsheet contains all the content of the Data matrix plus as many blank lines as there were blank Ids.
For example, if the Data matrix had 1000 lines and 5 Ids in blank, I had an array with 995 lines, but the resulting Excel spreadsheet is getting the 995 lines of the Data matrix plus 5 lihas in white at the end!
Whereas I need to send a spreadsheet xls (not xlsx) blank lines, how can I fix this?
From now on, thank you very much.
The variable
id
is a string, number...? If you provide part of the database withdput(head(dados))
would help clarify.– neves
If
id == NA
, then you can filter your data frame before saving to Excel. https://answall.com/questions/87730/comorremover-linha-que-tem-missing/87872#87872 . Or usena.omit()
– bbiasi
That is if
id == NA
. It may be thatid == ""
, for example. Since AP did not specify what blank id means, we cannot guess.– Marcus Nunes
Let’s go: ID is a numerical column, with some blank cells. The first two lines of the code I posted are the data.frame. And the problem was much more strange, because as a test I replaced the filter of the first two lines by
k <- which(Dados[,2]==5)
andDados <- Dados[k,]
and the result was an Excel spreadsheet with lines that have 5 in the second column at the top and all other lines below.– Marcelo
But the Data matrix was formed from commands and operations on other matrices. As a test, I applied the
is.na
in a step further back and it worked, the problem was solved. Anyway, thank you so much for the suggestions.– Marcelo
For the record: Blank ID means NA.
– Marcelo