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 ...
我想转换' prop_country_id'到了角色'数据类型。 因此,我已实施此转化代码。
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" ...
并保存文件。
write.csv(stratified_1, "train.csv", row.names = FALSE)
然而,一旦我读回文件,' prop_country_id'是整数'再回来。
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 ...
如何使用' prop_country_id'保存文件?作为角色?
答案 0 :(得分:0)
这可能有用......您可以在csv中读取列的类:
stratified_1 = read.csv("train.csv", header = TRUE, colClasses=c("prop_country_id"="character"))