dataframe - Data format change while writing to a file connection R -
i have data, reproducible example set is:
a<-data.frame( mdse_item_i=as.factor(c(rep(10,2),rep(15,2),rep(23,4))), co_loc_i=as.factor(c(rep(1,2),rep(3,2),rep(5,1),rep(7,3))), date = seq(as.date("2011-12-01"), as.date("2011-12-08"),by="days"), sls_type_grp_c=as.factor(c(rep("r",2),rep("p",4),rep("p",2))), non_clr_sls_q=as.numeric(rnorm(8,3,1)))
which trying write file connection using cat command:
fileconn<-file("/users/z076156/desktop/output.txt") sapply(1:nrow(a), fun = function(row) { cat(a$mdse_item_i[row],",",a$co_loc_i[row],",",a$date[row],",", a$sls_type_grp_c[row],",",a$non_clr_sls_q[row],"\n",sep='')}) close(fileconn)
the output is:
1,1,15309,2,3.043244 1,1,15310,2,2.667343 2,2,15311,1,1.177765 2,2,15312,1,4.411262 3,3,15313,1,2.162418 3,4,15314,1,1.876237 3,4,15315,1,6.043766 3,4,15316,1,3.235021
this output stripped of factor
, date
formatting. how can maintain formatting while writing file connection?
maybe as.character() help, in
> cat(a$date) 15309 15310 15311 15312 15313 15314 15315 15316> > > cat(as.character(a$date)) 2011-12-01 2011-12-02 2011-12-03 2011-12-04 2011-12-05 2011-12-06 2011-12- 07 2011-12-08> >
also
> cat(a$mdse_item_i) 1 1 2 2 3 3 3 3> > cat(as.character(a$mdse_item_i)) 10 10 15 15 23 23 23 23>
hth, bernhard
Comments
Post a Comment