[R] Lose of decimal when using write.table to text file
Dieter Menne
dieter.menne at menne-biomed.de
Thu Jun 25 10:14:55 CEST 2009
Bob Ly <robertly <at> vfemail.net> writes:
> I have the following:
> >Date<-c("08/05/08","08/06/08","08/07/08")
> >Weight<-c(209.4,211.8,210.0)
> >planned.meal<-cbind(Date,Weight)
> >planned.meal
> Date Weight
> 1 08/05/08, 209.4
> 2 08/06/08, 211.8
> 3 08/07/08, 210.0
This is strange. When I run your code, I get
> planned.meal
Date Weight
[1,] "08/05/08" "209.4"
[2,] "08/06/08" "211.8"
[3,] "08/07/08" "210"
This could be a global date format setting, but it show part of the problem:
everything is converted to string as the common denominator.
Better use a data frame. And (from docs)
"For finer control, use format to make a character matrix/data frame, and call
write.table on that"
Dieter
Date<-c("08/05/08","08/06/08","08/07/08")
Weight<-c(209.4,211.8,210.0)
planned.meal<-cbind(Date,Weight)
planned.meal
str(planned.meal)
planned.meal<-data.frame(Date=Date,Weight=Weight)
planned.meal
str(planned.meal)
options(digits=1)
planned.meal.format = format(planned.meal,nsmall=1)
planned.meal.format
write.table(planned.meal.format,
file="plannedMeal1.txt", quote=FALSE, row.names=FALSE)
More information about the R-help
mailing list