crazy basic question

the chunk below is part of initial cleaning; Im sorry to look foolish, but Im having a hard time rationalizing why "select' in the last line returns an error when the variable was renamed above. If I replace "id' with "ride_id' (the original name), it runs fine. Does this relate to working in .rmd?

names(yr1)
yr1 %>%
  rename("id" = "ride_id", "type" = "rideable_type", "start time" = "started_at",
         "end time" = "ended_at", "start st id" = "start_station_id", "start st     
         nm"= "start_station_name", "end st nm" = "end_station_name", "end st id" =
         "end_station_id", "st lat" = "start_lat", "st long" = "start_lng", "end lat" = "end_lat","end long"="end_lng","mem type" = "member_casual")
names(yr1)
glimpse(yr1)
tibble(yr1)
yr1$duration <- difftime(yr1$ended_at,yr1$started_at)
tibble(yr1)
str(yr1)
yr1 %>%
  select(id)

You need to assign the output of your rename statement to yr1:

yr1 <- yr1 %>%
  rename("id" = "ride_id", "type" = "rideable_type", "start time" = "started_at",
         "end time" = "ended_at", "start st id" = "start_station_id", "start st     
         nm"= "start_station_name", "end st nm" = "end_station_name", "end st id" =
         "end_station_id", "st lat" = "start_lat", "st long" = "start_lng", "end lat" = "end_lat","end long"="end_lng","mem type" = "member_casual")
2 Likes

It looks like you haven't save the renamed series back into yr1. Like most functions, rename gives a new copy of the input--it doesn't change the original.

thanks! Is that because I utilized pipe only?

Not really.

Your original statement is the equivalent of:

rename(yr1, "id" = "ride_id", "type" = "rideable_type", "start time" = "started_at",
         "end time" = "ended_at", "start st id" = "start_station_id", "start st     
         nm"= "start_station_name", "end st nm" = "end_station_name", "end st id" =
         "end_station_id", "st lat" = "start_lat", "st long" = "start_lng", "end lat" = "end_lat","end long"="end_lng","mem type" = "member_casual")

The new one is the equivalent of:

yr1 <-
  rename(yr1, "id" = "ride_id", "type" = "rideable_type", "start time" = "started_at",
         "end time" = "ended_at", "start st id" = "start_station_id", "start st     
         nm"= "start_station_name", "end st nm" = "end_station_name", "end st id" =
         "end_station_id", "st lat" = "start_lat", "st long" = "start_lng", "end lat" = "end_lat","end long"="end_lng","mem type" = "member_casual")

You need to assign the output to an object, which in this case is to overwrite yr1.

1 Like

now I get it; Thanks so much Martin

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.