0

I have several hundred files regarding information in .pet files organized by date code (19960101 is format YYYYMMDD). I'm trying to add a column, NDate with the date code:

for (pet.atual in files.pet) {
  data.pet.atual <-
    read.table(file = pet.atual,
               header = FALSE,
               sep = ",",
               quote = "\"",           
               comment.char = ";");     
    data.pet.atual <- cbind(data.pet.atual, NDate= pet.atual)
}

What i'm trying to achieve, for example, is for the 01-01-1996 NDate = 19960101, for 02-01-1996 NDate = 19960102 and so on. Still the for loop just replaces the NDate field everytime it runs with the latest pet.atual, ideas? Thanks

2
  • 1
    You should probably use lapply instead of a for loop with a growing set of columns.
    – Frank
    Mar 15, 2017 at 19:50
  • It's not only theNDate field which is overwritten in each pass of the for loop but also data.pet.atual.
    – Uwe
    Mar 15, 2017 at 23:04

3 Answers 3

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

2

Small modification should do the trick:

data.pet.atual <- NULL
for (pet.atual in files.pet) {
    tmp.data <-
    read.table(file = pet.atual,
               header = FALSE,
               sep = ",",
               quote = "\"",           
               comment.char = ";");     
    tmp.data <- cbind(tmp.data, NDate= pet.atual)
    data.pet.atual <- rbind(data.pet.atual, tmp.data)
}

You can also replace the tmp.data<-cbind(...) by tmp.data$NDate <- pet.atual

2

You may also try fread() and rbindlist() from the data.table package (untested due to lack of a reproducible example):

library(data.table)
result <- rbindlist(lapply(files.pet, fread), idcol = "NDate")
result[, NDate := anytime::anydate(files.pet[NDate])]

lapply() "loops" over all entries in files.pet executing fread() for each entry and returns a list with the data.tables fread has created from reading each file. rbindlist() is used to combine all pieces into one large data.table. The parameter idcol = NDate generates an index column named NDate to identify the origin of each row in the final output. The ids are integer numbers 1 to the length of the list (if the list is not named).

Finally, the id number is used to lookup the file name in files.pet which is directly converted to class Date using the anytime package.
EDIT Perhaps, it might be more efficient to convert the file names to Date first before looking them up:

result[, NDate := anytime::anydate(files.pet)[NDate]]

Although fread() is pretty smart in analysing and guessing the right parameters for reading the files it might be necessary (and perhaps faster as well) to supply additional parameters, e.g.:

result <- rbindlist(lapply(files.pet, fread, header = FALSE, sep = ","), idcol = "NDate")
1

Yes, lapply will help, as Frank suggests. And you want to use rbind to keep the dates different for each file. Something along the lines of:

I'm assuming files.pet is a list of all the files you want to include...

my.fun<-function(file){
data <- read.table(file = file,
           header = FALSE,
           sep = ",",
           quote = "\"",           
           comment.char = ";")     
data$NDate = file
return(data)}

data.pet.atual <- do.call(rbind.data.frame, lapply(files.pet, FUN=my.fun))

I can't test this without a reproducible example, so you may need to play with it a bit, but the general approach should work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.