0

I have a problem with special characters in a column of a table.

Here is an example of the data:

structure(list(shipType = structure(c(1L, 3L, 1L, 2L, 4L), .Label = c("CARGO", 
"FISHING", "TOWING_LONG_WIDE", "UNKNOWN"), class = "factor"), 
    shipCargo = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "UNDEFINED", class = "factor"), 
    destination = structure(c(3L, 1L, 2L, 4L, 5L), .Label = c("\\KORSOR  ;.,NA,.\\", 
    "LEHTMA", "RIGA", "TALLIN", "VYBORG"), class = "factor"), 
    eta = structure(c(1L, 2L, 5L, 3L, 4L), .Label = c("01/01 00:00 UTC", 
    "01/01 09:00 UTC", "24/12 16:00 UTC", "26/12 07:00 UTC", 
    "30/12 16:00 UTC"), class = "factor"), imo = structure(c(3L, 
    5L, 1L, 4L, 2L), .Label = c("7101891", "7406318", "9066045", 
    "9158185", "Russia"), class = "factor"), callsign = structure(c(5L, 
    1L, 2L, 3L, 4L), .Label = c("12", "UALB", "UBYK8", "UFPC", 
    "UICC"), class = "factor"), country = structure(c(2L, 1L, 
    2L, 2L, 2L), .Label = c("2014-12-29", "Russia"), class = "factor"), 
    month = c(12L, 1L, 12L, 12L, 12L), date = structure(c(2L, 
    1L, 2L, 2L, 2L), .Label = c("", "2014-12-29"), class = "factor"), 
    week = c(1L, NA, 1L, 1L, 1L), X = c(NA, NA, NA, NA, NA)), .Names = c("shipType", 
"shipCargo", "destination", "eta", "imo", "callsign", "country", 
"month", "date", "week", "X"), class = "data.frame", row.names = c(NA, 
-5L))

As you can see on the second row, there is a problem in the column "destination" when reading the file with the following code

data <- read.table(file, header=T, fill=T, sep=",")

I have tried different things, such as: exporting with quotes and without headers

data <- read.table(file, sep=",", fill=T, head=F, quote="")

and then removing the first line (the actual headers that are in the table...) and adding one more time these headers

data <- data[-1,]
colnames(data)<-c( "shipType", "shipCargo","destination","eta","imo","callsign", "country","month","date","week")

It looks better, but there are a lot of special characters and it will be time consuming / source of errors (I have lot of tables..) to edit.

Is there a way to avoid the columns to be messed up when importing the file?

Thank you!

Floni
  • 475
  • 2
  • 13

0 Answers0