Label of the variables cannot be changed in R

Viewed 54

I have in a folder 1000 .csv files. The problem is that the variables emmisions, price, airline, luggage and duration are a kind of moved. In some files, they are placed right and in some not. I have problem with this code because It does not change the label of the variables how I expect. Is there something wrong in the code?

library(fs)
library(tidyverse)
library(purrr)
`%||%` <- rlang::`%||%`



path_i <- "x/x/x"



flight_info <- "x/x/x" %>%
  dir_ls(recurse = TRUE, type = "file") %>% 
  as_tibble_col("path") %>% 
  slice_head(n=100) %>% 
  mutate(data = map(path, function(path_i){
    
    path_i <<- path_i
    
    data_i <- read_csv(path_i, show_col_types=FALSE)
    
    data_i$date <- as.character(data_i[["date"]] %||% NA_character_)

    
    moved_vars <- tibble()
    
  if(all(str_detect(data_i[["airline"]] %||% "", "€"))) {
    
    moved_vars <- bind_rows(moved_vars, select(data_i, price = airline))
  }
    
    
  if(all(str_detect(data_i[["price"]] %||% "", "hr|min"))){
    
    moved_vars <- bind_rows(moved_vars, select(data_i, duration = price))
  }
    
 
    
  if(all(str_detect(data_i[["emissions"]] %||% "", "[included]"))){
    
    moved_vars <- bind_rows(moved_vars, select(data_i, luggage = emissions))
  }
    
    
  if(mean(str_detect(data_i[["luggage"]] %||% "", "(?i)CO2"), na.rm=TRUE )>0.8){
    
    moved_vars <- bind_rows(moved_vars, select(data_i, emissions = luggage))
  }
    
  if(mean(str_detect(data_i[["duration"]] %||% "",  "^[[:alpha:][:space:]]+$"), na.rm=TRUE)>0.8){
    
    moved_vars <- bind_rows(moved_vars, select(data_i, airlines = duration))
  }
    
  data_i_mod <- bind_rows(
    moved_vars, select(data_i, -any_of(colnames(moved_vars)))
  )
    
  return(data_i)
  }))



Here, there is a reproducible example

structure(list(depart = c("ESB", "ESB", "LUX", "LUX", "LUX", 
"LUX"), destination = c("BEG", "BEG", "LIS", "LIS", "LIS", "LIS"
), departue_info = c("9:05 AM on Thu Mar 17", "9:05 AM on Thu Mar 17", 
"7:55 PM on Thu Mar 17", "6:00 AM on Thu Mar 17", "11:50 AM on Thu Mar 17", 
"6:30 PM on Thu Mar 17"), arrival_info = c("9:05 AM on Thu Mar 17", 
"9:05 AM on Thu Mar 17", "9:45 PM on Thu Mar 17", "7:50 AM on Thu Mar 17", 
"1:40 PM on Thu Mar 17", "8:20 PM on Thu Mar 17"), price = c("2 hr", 
"2 hr", "2 hr 50 min", "2 hr 50 min", "2 hr 50 min", "2 hr 50 min"
), airline = c("\200259", "\200259", "\200130", "\200140", "\200165", 
"\200334"), duration = c("Turkish AirlinesAir SerbiaOperated by Anadolujet Trademark of Turkish Airlines", 
"Turkish AirlinesAir SerbiaOperated by Anadolujet Trademark of Turkish Airlines", 
"easyJet", "LuxairTap Air Portugal", "Tap Air PortugalLuxair", 
"Ryanair"), emissions = c("Included", "Included", "Not included", 
"Included", "Included", "Not included"), luggage = c("135 kg CO2", 
"135 kg CO2", "175 kg CO2-26% emissions", "178 kg CO2-25% emissions", 
"155 kg CO2-35% emissions", "185 kg CO2-22% emissions"), description = c("From 259 euros. Nonstop flight with Turkish Airlines. Operated by Anadolujet Trademark of Turkish Airlines. Leaves Ankara Esenboga Airport at 9:05 AM on Thursday, March 17 and arrives at Nikola Tesla Airport at 9:05 AM on Thursday, March 17. Total duration 2 hr.  Carbon emissions estimate: 135 kilograms. Select flight", 
"From 259 euros. Nonstop flight with Turkish Airlines. Operated by Anadolujet Trademark of Turkish Airlines. Leaves Ankara Esenboga Airport at 9:05 AM on Thursday, March 17 and arrives at Nikola Tesla Airport at 9:05 AM on Thursday, March 17. Total duration 2 hr.  Carbon emissions estimate: 135 kilograms. Select flight", 
"From 130 euros.This price does not include overhead bin access. Nonstop flight with easyJet. Leaves Luxembourg Airport at 7:55 PM on Thursday, March 17 and arrives at Humberto Delgado Airport at 9:45 PM on Thursday, March 17. Total duration 2 hr 50 min.  Select flight", 
"From 140 euros. Nonstop flight with Luxair. Leaves Luxembourg Airport at 6:00 AM on Thursday, March 17 and arrives at Humberto Delgado Airport at 7:50 AM on Thursday, March 17. Total duration 2 hr 50 min.  Select flight", 
"From 165 euros. Nonstop flight with Tap Air Portugal. Leaves Luxembourg Airport at 11:50 AM on Thursday, March 17 and arrives at Humberto Delgado Airport at 1:40 PM on Thursday, March 17. Total duration 2 hr 50 min.  Select flight", 
"From 334 euros.This price does not include overhead bin access. Nonstop flight with Ryanair. Leaves Luxembourg Airport at 6:30 PM on Thursday, March 17 and arrives at Humberto Delgado Airport at 8:20 PM on Thursday, March 17. Total duration 2 hr 50 min.  Select flight"
), dates = structure(c(19068, 19068, 19068, 19068, 19068, 19068
), class = "Date"), time_request = structure(c(27000, 27000, 
27000, 27000, 27000, 27000), class = c("hms", "difftime"), units = "secs"), 
    date_request = c("16/03/22", "16/03/22", "16/03/22", "16/03/22", 
    "16/03/22", "16/03/22"), URL = c("https://www.google.com/travel/flights?q=flights%20from%20ESB%20%20to%20BEG%20%20on%202022-03-17%20oneway%20nonstop%20economy", 
    "https://www.google.com/travel/flights?q=flights%20from%20ESB%20%20to%20BEG%20%20on%202022-03-17%20oneway%20nonstop%20economy", 
    "https://www.google.com/travel/flights?q=flights%20from%20LUX%20%20to%20LIS%20%20on%202022-03-17%20oneway%20nonstop%20economy", 
    "https://www.google.com/travel/flights?q=flights%20from%20LUX%20%20to%20LIS%20%20on%202022-03-17%20oneway%20nonstop%20economy", 
    "https://www.google.com/travel/flights?q=flights%20from%20LUX%20%20to%20LIS%20%20on%202022-03-17%20oneway%20nonstop%20economy", 
    "https://www.google.com/travel/flights?q=flights%20from%20LUX%20%20to%20LIS%20%20on%202022-03-17%20oneway%20nonstop%20economy"
    ), date = c(NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

Desired outcome


structure(list(depart = c("ESB", "ESB", "LUX", "LUX", "LUX", 
"LUX"), destination = c("BEG", "BEG", "LIS", "LIS", "LIS", "LIS"
), departue_info = c("9:05 AM on Thu Mar 17", "9:05 AM on Thu Mar 17", 
"7:55 PM on Thu Mar 17", "6:00 AM on Thu Mar 17", "11:50 AM on Thu Mar 17", 
"6:30 PM on Thu Mar 17"), arrival_info = c("9:05 AM on Thu Mar 17", 
"9:05 AM on Thu Mar 17", "9:45 PM on Thu Mar 17", "7:50 AM on Thu Mar 17", 
"1:40 PM on Thu Mar 17", "8:20 PM on Thu Mar 17"), duration = c("2 hr", 
"2 hr", "2 hr 50 min", "2 hr 50 min", "2 hr 50 min", "2 hr 50 min"
), price = c("\200259", "\200259", "\200130", "\200140", "\200165", 
"\200334"), airline = c("Turkish AirlinesAir SerbiaOperated by Anadolujet Trademark of Turkish Airlines", 
"Turkish AirlinesAir SerbiaOperated by Anadolujet Trademark of Turkish Airlines", 
"easyJet", "LuxairTap Air Portugal", "Tap Air PortugalLuxair", 
"Ryanair"), luggage = c("Included", "Included", "Not included", 
"Included", "Included", "Not included"), emmisions = c("135 kg CO2", 
"135 kg CO2", "175 kg CO2-26% emissions", "178 kg CO2-25% emissions", 
"155 kg CO2-35% emissions", "185 kg CO2-22% emissions"), description = c("From 259 euros. Nonstop flight with Turkish Airlines. Operated by Anadolujet Trademark of Turkish Airlines. Leaves Ankara Esenboga Airport at 9:05 AM on Thursday, March 17 and arrives at Nikola Tesla Airport at 9:05 AM on Thursday, March 17. Total duration 2 hr.  Carbon emissions estimate: 135 kilograms. Select flight", 
"From 259 euros. Nonstop flight with Turkish Airlines. Operated by Anadolujet Trademark of Turkish Airlines. Leaves Ankara Esenboga Airport at 9:05 AM on Thursday, March 17 and arrives at Nikola Tesla Airport at 9:05 AM on Thursday, March 17. Total duration 2 hr.  Carbon emissions estimate: 135 kilograms. Select flight", 
"From 130 euros.This price does not include overhead bin access. Nonstop flight with easyJet. Leaves Luxembourg Airport at 7:55 PM on Thursday, March 17 and arrives at Humberto Delgado Airport at 9:45 PM on Thursday, March 17. Total duration 2 hr 50 min.  Select flight", 
"From 140 euros. Nonstop flight with Luxair. Leaves Luxembourg Airport at 6:00 AM on Thursday, March 17 and arrives at Humberto Delgado Airport at 7:50 AM on Thursday, March 17. Total duration 2 hr 50 min.  Select flight", 
"From 165 euros. Nonstop flight with Tap Air Portugal. Leaves Luxembourg Airport at 11:50 AM on Thursday, March 17 and arrives at Humberto Delgado Airport at 1:40 PM on Thursday, March 17. Total duration 2 hr 50 min.  Select flight", 
"From 334 euros.This price does not include overhead bin access. Nonstop flight with Ryanair. Leaves Luxembourg Airport at 6:30 PM on Thursday, March 17 and arrives at Humberto Delgado Airport at 8:20 PM on Thursday, March 17. Total duration 2 hr 50 min.  Select flight"
), dates = structure(c(19068, 19068, 19068, 19068, 19068, 19068
), class = "Date"), time_request = structure(c(27000, 27000, 
27000, 27000, 27000, 27000), class = c("hms", "difftime"), units = "secs"), 
    date_request = c("16/03/22", "16/03/22", "16/03/22", "16/03/22", 
    "16/03/22", "16/03/22"), URL = c("https://www.google.com/travel/flights?q=flights%20from%20ESB%20%20to%20BEG%20%20on%202022-03-17%20oneway%20nonstop%20economy", 
    "https://www.google.com/travel/flights?q=flights%20from%20ESB%20%20to%20BEG%20%20on%202022-03-17%20oneway%20nonstop%20economy", 
    "https://www.google.com/travel/flights?q=flights%20from%20LUX%20%20to%20LIS%20%20on%202022-03-17%20oneway%20nonstop%20economy", 
    "https://www.google.com/travel/flights?q=flights%20from%20LUX%20%20to%20LIS%20%20on%202022-03-17%20oneway%20nonstop%20economy", 
    "https://www.google.com/travel/flights?q=flights%20from%20LUX%20%20to%20LIS%20%20on%202022-03-17%20oneway%20nonstop%20economy", 
    "https://www.google.com/travel/flights?q=flights%20from%20LUX%20%20to%20LIS%20%20on%202022-03-17%20oneway%20nonstop%20economy"
    ), date = c(NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))
0 Answers
Related