Trouble converting data type.

Unable to convert data type.
the start_station_id is in double format, wanted to convert it to a character type.

        #conversion
         ds1$start_station_id <- as.character(ds1$start_station_id)

after running this code and checking the dataset using str(ds1), it still displays as a double type.
where did I go wrong?.

I do not see a problem with your code. Can you post your data? The output of

dput(head(ds1))

is a good way to share your data.

Here is my test.

ds1 <- data.frame(start_station_id=1:4)
str(ds1)
'data.frame':	4 obs. of  1 variable:
 $ start_station_id: int  1 2 3 4
ds1$start_station_id <- as.character(ds1$start_station_id)
str(ds1)
'data.frame':	4 obs. of  1 variable:
 $ start_station_id: chr  "1" "2" "3" "4

Sorry, I'm new to the community, could you tell me how to load the data.

but here is the link to the divvy site where I got the data set in csv form; it's named 202011-divvy-tripdata.zip

Run

dput(head(ds1))

and paste the output into a reply to this thread.

1 Like

dput(head(ds1))
structure(list(ride_id = c("BD0A6FF6FFF9B921", "96A7A7A4BDE4F82D",
"C61526D06582BDC5", "E533E89C32080B9E", "1C9F4EF18C168C60", "7259585D8276D338"
), rideable_type = c("electric_bike", "electric_bike", "electric_bike",
"electric_bike", "electric_bike", "electric_bike"), started_at = structure(c(1604237760,
1604225006, 1604190845, 1604191516, 1604245405, 1605369317), tzone = "UTC", class = c("POSIXct",
"POSIXt")), ended_at = structure(c(1604238340, 1604225685, 1604192586,
1604192071, 1604247412, 1605372278), tzone = "UTC", class = c("POSIXct",
"POSIXt")), start_station_name = c("Dearborn St & Erie St", "Franklin St & Illinois St",
"Lake Shore Dr & Monroe St", "Leavitt St & Chicago Ave", "Buckingham Fountain",
"Wabash Ave & 16th St"), start_station_id = c("110", "672", "76",
"659", "2", "72"), end_station_name = c("St. Clair St & Erie St",
"Noble St & Milwaukee Ave", "Federal St & Polk St", "Stave St & Armitage Ave",
"Buckingham Fountain", "Lake Shore Dr & Monroe St"), end_station_id = c(211,
29, 41, 185, 2, 76), start_lat = c(41.8941765, 41.8909586666667,
41.8809828333333, 41.8954991666667, 41.8764973333333, 41.8602888333333
), start_lng = c(-87.6291273333333, -87.6353428333333, -87.6167541666667,
-87.682013, -87.620358, -87.625806), end_lat = c(41.8944341666667,
41.900675, 41.8720545, 41.9177445, 41.8764483333333, 41.880985
), end_lng = c(-87.6233791666667, -87.6624803333333, -87.6295503333333,
-87.6913918333333, -87.620338, -87.6167735), member_casual = c("casual",
"casual", "casual", "casual", "casual", "casual")), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))

the start_station_id begins at line 11

start_station_id is already characters in the data you posted.

1 Like

Oh thanks, I just noticed. It's shown as double when I use str

str(ds1)  
    ride_id = col_character(),
  ..   rideable_type = col_character(),
  ..   started_at = col_datetime(format = ""),
  ..   ended_at = col_datetime(format = ""),
  ..   start_station_name = col_character(),
  ..   start_station_id = col_double(),
  ..   end_station_name = col_character(),
  ..   end_station_id = col_double(),
  ..   start_lat = col_double(),
  ..   start_lng = col_double(),
  ..   end_lat = col_double(),
  ..   end_lng = col_double(),
  ..   member_casual = col_character()
  .. )

but when I use class, it's shown as a character.

class(ds1$start_station_id)
[1] "character"

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.