2015-08-12 13 views
6

dati campione (emp.data)Come leggere il file separato da tabulazione in data.table usando fread?

Beth 4.00 0 
Dan 3.75 0 
Kathy 4.00 10 
Mark 5.00 20 
Mary 5.50 22 
Susie 4.25 18 

posso leggere in un data.frame utilizzando read.table, quindi convertirlo in data.table:

library(data.table) 
df <- read.table("emp.data", col.names = c("Name", "PayRate", "HoursWorked")) 
DT <- as.data.table(df, key = HoursWorked) 

calcolare la retribuzione (filtrare zero ore):

DT[HoursWorked > 0, .(Name, Pay = PayRate * HoursWorked),] 

    Name Pay 
1: Kathy 40.0 
2: Mark 100.0 
3: Mary 121.0 
4: Susie 76.5 

Che funziona bene; tuttavia, ritengo che ci sia un ulteriore passaggio nella conversione. Poiché c'è fread() in data.table, perché non utilizzarlo direttamente?

readDT <- fread("emp.data", header=FALSE, sep="\t") 

       V1 
1: Beth 4.00 0 
2: Dan 3.75 0 
3: Kathy 4.00 10 
4: Mark 5.00 20 
5: Mary 5.50 22 
6: Susie 4.25 18 

str(readDT) 
Classes 'data.table' and 'data.frame': 6 obs. of 1 variable: 
$ V1: chr "Beth 4.00 0" "Dan 3.75 0" "Kathy 4.00 10" "Mark 5.00 20" ... 
- attr(*, ".internal.selfref")=<externalptr> 

I dati sono riconosciuti come una colonna; ovviamente questo non funziona

Domanda

Come leggere questi dati utilizzando fread() correttamente? (Se possibile, impostare i nomi delle colonne pure.)

+0

cercare di non specificare il 'sep' e lasciarlo "auto"(per non' fread' decidere). In altre parole, basta 'fread (" emp.data ", header = FALSE)' –

+0

@DavidArenburg Grazie. Ho ricevuto questo errore: '> readDT <- fread (" emp.data ", header = FALSE) Errore in fread (" emp.data ", header = FALSE): Non posizionato correttamente dopo il test del formato della riga di intestazione. ch = '' ' – Nick

+0

È difficile dirlo, puoi fornire un' dput' del tuo set di dati? Forse anche provare senza specificare 'header' –

risposta

5

Questo è stato risolto di recente nella versione devel, v1.9.5 (sarà presto disponibile sul CRAN come v1.9.6):

require(data.table) # v1.9.5+ 
fread("~/Downloads/tmp.txt") 
#  V1 V2 V3 
# 1: Beth 4.00 0 
# 2: Dan 3.75 0 
# 3: Kathy 4.00 10 
# 4: Mark 5.00 20 
# 5: Mary 5.50 22 
# 6: Susie 4.25 18 

Vedi README.md nella pagina del progetto per maggiori informazioni. fread ha ottenuto l'argomento strip.white (in mezzo ad altre funzionalità/correzioni di errori), che di default è TRUE.


Aggiornamento: ha anche col.names argomento ora:

fread("~/Downloads/tmp.txt", col.names = c("Name", "PayRate", "HoursWorked")) 
#  Name PayRate HoursWorked 
# 1: Beth 4.00   0 
# 2: Dan 3.75   0 
# 3: Kathy 4.00   10 
# 4: Mark 5.00   20 
# 5: Mary 5.50   22 
# 6: Susie 4.25   18 
+1

La ringrazio molto per la risposta e per aver reso 'data.table' un ottimo strumento così. anche 'col.names' è molto utile! – Nick

5

Utilizzando awk per rimuovere gli spazi bianchi e poi la lettura con fread ha lavorato per me.

DT <- fread("awk '{$1=$1}1' emp.data") 
DT 
#  V1 V2 V3 
#1: Beth 4.00 0 
#2: Dan 3.75 0 
#3: Kathy 4.00 10 
#4: Mark 5.00 20 
#5: Mary 5.50 22 
#6: Susie 4.25 18 

str(DT) 
#Classes ‘data.table’ and 'data.frame': 6 obs. of 3 variables: 
#$ V1: chr "Beth" "Dan" "Kathy" "Mark" ... 
#$ V2: num 4 3.75 4 5 5.5 4.25 
#$ V3: int 0 0 10 20 22 18 
# - attr(*, ".internal.selfref")=<externalptr> 

sono stato in grado di replicare lo stesso problema con il codice del PO

fread("emp.data", header=FALSE, sep="\t") 
#    V1 
#1: Beth 4.00 0 
#2: Dan 3.75 0 
#3: Kathy 4.00 10 
#4: Mark 5.00 20 
#5: Mary 5.50 22 
#6: Susie 4.25 18 
+0

Grazie, ho appena letto la risposta, con mia sorpresa che il file read.table è in grado di gestire questo problema senza problemi Forse 'read.table' è più intelligente – Nick

+0

@Nick' read. tavolo/read.csv' dovrebbe gestire questa situazione. non sono sicuro che il motivo per cui 'fread' ha questo tipo di problema. – akrun

+1

penso y il nostro approccio con 'awk' va bene; tuttavia, attualmente sono su Windows e la versione di 'awk' su Windows non funziona. Così ho provato a ripulire con il Perl: 'perl -pe 's/{1,} /,/g' emp.data> emp.data.clean.perl', poi leggerlo con' fread'. Controllerò il tuo approccio awk quando torno a Linux. – Nick

Problemi correlati