Could anyone help in, How to read below .txt file( such files are in thousands) in tabular form and convert the rownames into Variables names along with DataType
Is this file complete and in the way, your actual *.txt files are saved?
Your last two lines will cause problems, since the rowname Time_taken (min) is poorly chosen. Especially the last line with two ":" will break the reading.
However, if you always have 18 rows, you could just do this and skip the last line:
library(tidyverse)
data <- read_delim('given_file.txt', col_names = FALSE, n_max = 18) |>
pivot_wider(names_from = X1, values_from = X2) |>
mutate(Time_taken = str_extract(string = Time_taken, "[0-9\\.,]+"))
#> Warning: One or more parsing issues, see `problems()` for details
#> Rows: 18 Columns: 2
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: " "
#> chr (2): X1, X2
#>
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
data
#> # A tibble: 1 × 18
#> Delivery_per…¹ Deliv…² Resta…³ Resta…⁴ Deliv…⁵ Deliv…⁶ Order…⁷ Time_…⁸ Time_…⁹
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 22.000000 4.7000… 18.530… 73.828… 18.560… 73.858… 01-03-… 23:35 23:40
#> # … with 9 more variables: Weather <chr>, Road_traffic_density <chr>,
#> # Vehicle_condition <chr>, Type_of_order <chr>, Type_of_vehicle <chr>,
#> # multiple_deliveries <chr>, Festival <chr>, City <chr>, Time_taken <chr>,
#> # and abbreviated variable names ¹Delivery_person_Age,
#> # ²Delivery_person_Ratings, ³Restaurant_latitude, ⁴Restaurant_longitude,
#> # ⁵Delivery_location_latitude, ⁶Delivery_location_longitude, ⁷Order_Date,
#> # ⁸Time_Orderd, ⁹Time_Order_picked
#> # ℹ Use `colnames()` to see all variable names