how to maintain conversion from number to character in R after writing to file -


str(stratified_1)  'data.frame':   60197 obs. of  30 variables:  $ srch_id                    : int  6 52 61 81 106 118 119 139 151 160 ...  $ date_time                  : int  8500 15379 42277 48907 31091 32805 16006 23669 15969 23629 ...  $ site_id                    : int  14 16 5 5 5 5 23 24 26 22 ...  $ visitor_location_country_id: int  100 31 219 219 219 219 181 219 39 92 ...  $ visitor_hist_starrating    : int  272 272 272 272 272 272 272 272 272 272 ...  $ visitor_hist_adr_usd       : int  2444 2444 2444 2444 2444 2444 2444 2444 2444 2444 ...  $ prop_country_id            : int  100 215 219 219 219 219 55 219 202 99 ... 

i want convert 'prop_country_id' 'character' data type. therefore, i've implemented conversion code.

stratified_1$prop_country_id = as.character(stratified_1$prop_country_id) str(stratified_1)  'data.frame':   60197 obs. of  30 variables:  $ srch_id                    : int  6 52 61 81 106 118 119 139 151 160 ...  $ date_time                  : int  8500 15379 42277 48907 31091 32805 16006 23669 15969 23629 ...  $ site_id                    : int  14 16 5 5 5 5 23 24 26 22 ...  $ visitor_location_country_id: int  100 31 219 219 219 219 181 219 39 92 ...  $ visitor_hist_starrating    : int  272 272 272 272 272 272 272 272 272 272 ...  $ visitor_hist_adr_usd       : int  2444 2444 2444 2444 2444 2444 2444 2444 2444 2444 ...  $ prop_country_id            : chr  "100" "215" "219" "219" ... 

and saved file.

write.csv(stratified_1, "train.csv", row.names = false)  

however, once read file back, 'prop_country_id' 'integer' again.

stratified_1 = read.csv("train.csv", header = true) str(stratified_1)  'data.frame':   60197 obs. of  30 variables:  $ srch_id                    : int  6 52 61 81 106 118 119 139 151 160 ...  $ date_time                  : int  8500 15379 42277 48907 31091 32805 16006 23669 15969 23629 ...  $ site_id                    : int  14 16 5 5 5 5 23 24 26 22 ...  $ visitor_location_country_id: int  100 31 219 219 219 219 181 219 39 92 ...  $ visitor_hist_starrating    : int  272 272 272 272 272 272 272 272 272 272 ...  $ visitor_hist_adr_usd       : int  2444 2444 2444 2444 2444 2444 2444 2444 2444 2444 ...  $ prop_country_id            : int  100 215 219 219 219 219 55 219 202 99 ... 

how can save file 'prop_country_id' character?

this may helpful... can specify class of column when read in csv:

stratified_1 = read.csv("train.csv", header = true, colclasses=c("prop_country_id"="character")) 

Comments