Konika Chawla
Konika Chawla

Reputation: 161

xlsx package in R converting numeric dataframe to text in xlsx file

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

Upvotes: 3

Views: 4764

Answers (1)

bergant
bergant

Reputation: 7232

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?

Upvotes: 1

Related Questions