Dates and export table

1

The first problem:

I have an input data file that contains dates in ddmmyy format, on which I must operate, and when I import it in R it recognizes it as a factor. What I have done is to convert it to a character using as.character and then I have transformed it to date using as.date , for example

df1$fcontrol<-as.Date(df1$fcontrol, format="%d%m%Y") 
df2$fparto<-as.Date(df2$fparto, format="%d%m%Y") 

df1$Var1<-as.numeric(df1$fcontrol - df2$fparto)

In all cases, the new date format in my work and output files is no longer "ddmmyy" but "yymmdd" and I do not know why it happens, if clearly in the order format I am specifying ddmmyy . What should I do?

The second problem

This is the output format I want my data to have:

ES06000011030003012012210220121360 4.07 4.38  678
ES06000011030003012012200320121460 3.46 4.81   20
ES0600001103000301201219042012 980 3.69 5.46   54
ES0600001103000301201212072012 660             32

Where the first 14 positions represent an ID, the next 8 are a date, the next 8 are another date, the next 4 parameters are of interest, the next 5 are parameters with a decimal separation, the next 5 are parameters3 with a decimal separation and the next 5 parameters4.

In R what I get using

write.table(Datos, "Datos_TD_LCR.DAT", row.names = FALSE, col.names=FALSE, quote = FALSE, sep="") 

is something of the type

ES060000110300030120122102201213604.074.38678
ES060000110300030120122003201214603.464.8120
ES06000011030003012012190420129803.695.4654
ES0600001103000301201212072012660NANA32 

And I do not know how to get what I need.

    
asked by Caro 12.07.2018 в 16:28
source

1 answer

1

First problem

You have a confusion, a data Date has no format, just because it is a date, the representation of the data and the data itself are two different things, when you do something like this:

> as.Date('12072018', format='%d%m%Y')
[1] "2018-07-12"

You are not saying that you want the date to have a certain format, but that the input string to convert has it. Any Date in R will be shown as in the previous example, with the ISO format ie yyyy-mm-dd , it is the best format to show this data since it is not ambiguous. Show the date as 07-12-2018 will have different interpretations depending on where you live. But remember, a date is still a date despite the way it is displayed. If you will eventually need to represent a date in a certain way, you should do it as a string, for example:

d <- as.Date('12072018', format='%d%m%Y')
format(d, "%d/%m/%Y")
[1] "12/07/2018"

Second problem

From what I understand of the file format you hope to get, it's from columns of fixed lengths. It is not so simple, in R there is not a routine that records this type of files, but the simplest thing is to incorporate the package gdata (obviously you have to install it before). For example, suppose we have the following data.frame :

df <- data.frame(NOMBRE=c('JUAN','JUAN', 'PEDRO', 'PEDRO', 'PEDRO', 'LUIS', 'LUIS'),
                 MONEDA=c('EUR', 'USD', 'EUR', 'CLP', 'USD', 'GBP', 'EUR'),
                 MONTO=c(10.1, 20.5, 30, 10, 20, 30, 10))

df

  NOMBRE MONEDA MONTO
1   JUAN    EUR  10.1
2   JUAN    USD  20.5
3  PEDRO    EUR  30.0
4  PEDRO    CLP  10.0
5  PEDRO    USD  20.0
6   LUIS    GBP  30.0
7   LUIS    EUR  10.0

And we want to generate a file with a specific format, for example, 5 characters for NOMBRE , 3 for MONEDA and 5 for MONTO , we could do it like this:

library(gdata)
write.fwf(df, file = "salida.txt", colnames = FALSE, width = c(5,3,5), sep="")

If we check the output file:

JUAN EUR 10.1
JUAN USD 20.5
PEDROEUR 30.0
PEDROCLP 10.0
PEDROUSD 20.0
LUIS GBP 30.0
LUIS EUR 10.0
    
answered by 12.07.2018 в 17:15