3

I have a dataframe in R consisting of 0s and 1s. When I write it to an xlsx file and open this file in microsoft spreadsheet, it show an error on each cell with number : "number stored as text". Is there a way to fix it in R. Here is a sample of what I did:

write.xlsx(result,"test.xlsx",sheetName="Sheet1",row.names=F) 
sapply(result,mode) 
GO_ids GO.0007411 GO.0006915 GO.0006914 GO.0006464 GO.0006298 GO.0018108 
 "numeric"  "numeric"  "numeric"  "numeric"  "numeric"  "numeric"  "numeric" 
GO.0006355 GO.0007155 GO.0030036 GO.0030155 
 "numeric"  "numeric"  "numeric"  "numeric"

Thanks

Jaap
  • 81,064
  • 34
  • 182
  • 193
Konika Chawla
  • 141
  • 2
  • 5
  • I don't believe the `write.xlsx()` function has that ability. But you could write a VBA macro in Excel to automatically convert to numeric... – CephBirk Feb 26 '15 at 15:43

1 Answers1

1

Your data is probably a factor:

> not_a_number <- factor(c(1, 2, 3))
> mode(not_a_number)
[1] "numeric"
> typeof(not_a_number)
[1] "integer"
> class(not_a_number)
[1] "factor"
> str(not_a_number)
 Factor w/ 3 levels "1","2","3": 1 2 3

write.xlsx writes factors as strings.

See also How to convert a data frame column to numeric type?

Community
  • 1
  • 1
bergant
  • 7,122
  • 1
  • 20
  • 24