Inaki Carril
Inaki Carril

Reputation: 61

How to change data types of multiple data frames in R

I have a list of multiple data frames.

files_list <- list(data_04, data_05, data_06, data_07, data_08, data_09, data_010, data_011,
                   data_012, data_013, data_015, data_016, data_017, data_018)

How do i change the data types of each column so that they match through data frames. All of the data frames have the same columns and column names. I have tried this but it doesn't work.

for (i in files_list) {
  i %>%
  mutate(ride_id = as.character(ride_id)) %>%
  mutate(rideable_type = as.character(rideable_type)) %>%
  mutate(started_at = as_datetime(started_at)) %>%
  mutate(ended_at = as_datetime(ended_at)) %>%
  mutate(start_station_name = as.character(start_station_name)) %>%
  mutate(start_station_id = as.integer(start_station_id)) %>%
  mutate(end_station_name = as.character(end_station_name)) %>%
  mutate(end_station_id = as.integer(end_station_id)) %>%
  mutate(start_lat = as.numeric(start_lat)) %>%
  mutate(start_lng = as.numeric(start_lng)) %>%
  mutate(end_lat = as.numeric(end_lat)) %>%
  mutate(end_lng = as.numeric(end_lng)) %>%
  mutate(member_casual = as.character(member_casual))
} 

Upvotes: 0

Views: 639

Answers (2)

TarJae
TarJae

Reputation: 79204

We may use type.convert(as.is = TRUE)

Try this shorter version of Tob's code: It will depend how date columns are saved in df!

EditDataFunction <- function(data){
    clean_data <- data %>% 
        as_tibble() %>% 
        type.convert(as.is = TRUE)
    
    return(clean_data)
}

clean_list <- lapply(files_list, EditDataFunction) 

Upvotes: 0

Tob
Tob

Reputation: 245

You can write a function and then use lapply() on the list.

The below should work but I can't be sure since I don't know what the data looks like.

EditDataFunction <- function(data){
  clean_data <- data %>% 
    mutate(ride_id = as.character(ride_id)) %>%
    mutate(rideable_type = as.character(rideable_type)) %>%
    mutate(started_at = as_datetime(started_at)) %>%
    mutate(ended_at = as_datetime(ended_at)) %>%
    mutate(start_station_name = as.character(start_station_name)) %>%
    mutate(start_station_id = as.integer(start_station_id)) %>%
    mutate(end_station_name = as.character(end_station_name)) %>%
    mutate(end_station_id = as.integer(end_station_id)) %>%
    mutate(start_lat = as.numeric(start_lat)) %>%
    mutate(start_lng = as.numeric(start_lng)) %>%
    mutate(end_lat = as.numeric(end_lat)) %>%
    mutate(end_lng = as.numeric(end_lng)) %>%
    mutate(member_casual = as.character(member_casual))
  
  return(clean_data)
  
}

clean_list <- lapply(files_list, EditDataFunction) 

Then if you need to bind them all together you could use data.table::rbindlist

Upvotes: 4

Related Questions