By flobrr


2018-06-13 09:37:17 8 Comments

I want to import multiple TSV Files (yes: TSV) in R.

Reading a single file with an selection of spacific columns works well by using:

data00<-read.csv(file = '/Volumes/2018/06_abteilungen/bi/analytics/tools/adobe/adobe_analytics/adobe_analytics_api_rohdaten/api_via_data_feed_auf_ftp/beispiel_datenexporte_data_feed/01sssamsung4de_20180501-000000.tsv',
                 sep ="\t",
             fill = TRUE,
             quote='', 
             header = FALSE 
)[ ,c(287, 288, 289, 290, 291, 292, 293, 304, 370, 661, 662, 812, 813,     994, 995, 1002)]

Now i want to import multiple files and combine them to a single dataframe:

setwd('/Volumes/2018/06_abteilungen/bi/analytics/tools/adobe/adobe_analytics/adobe_analytics_api_rohdaten/api_via_data_feed_auf_ftp/beispiel_datenexporte_data_feed/import_r')

temp <- list.files(pattern="*.tsv")

test_data <- lapply(temp, read.csv,
sep ="\t",
fill = TRUE,
quote='', 
header = FALSE 
)[ ,c(287, 288, 289, 290, 291, 292, 293, 304, 370, 661, 662, 812, 813, 994, 995, 1002)]

Last querie gives my an exception and doesnt work: Fehler in lapply(temp, read.csv, sep = "\t", fill = TRUE, quote = "", header = FALSE)[, : falsche Anzahl von Dimensionen (translation: wrong count of dimensions)

When I take all columns, it works:

test_data <- lapply(temp, read.csv,
sep ="\t",
fill = TRUE,
quote='', 
header = FALSE 
)

2 comments

@Wimpel 2018-06-13 09:45:37

Hard to say without sample-data, but I believe you have to 'merge' your imported lists first:

dplyr solution:

library(dplyr)
test_data <- lapply(temp, read.csv,
                    sep ="\t",
                    fill = TRUE,
                    quote='', 
                    header = FALSE 
                    ) %>%  
  bind_rows() %>%
  select( c(287, 288, 289, 290, 291, 292, 293, 304, 370, 661, 662, 812, 813, 994, 995, 1002) )

@Rohit 2018-06-13 09:43:18

You are indexing the list of data frames, and not the dataframes themselves. Try:

test_data <- lapply(temp,function(x){
  read.csv(file = x,
                 sep ="\t",
                 fill = TRUE,
                 quote='', 
                 header = FALSE 
  )[ ,c(287, 288, 289, 290, 291, 292, 293, 304, 370, 661, 662, 812, 813,994, 995, 1002)]
}
)

Related Questions

Sponsored Content

19 Answered Questions

[SOLVED] How to sort a dataframe by multiple column(s)

41 Answered Questions

[SOLVED] How to import an SQL file using the command line in MySQL?

54 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

9 Answered Questions

[SOLVED] How to import multiple .csv files at once?

  • 2012-07-11 13:10:50
  • Jojo Ono
  • 252253 View
  • 159 Score
  • 9 Answer
  • Tags:   r csv import r-faq

23 Answered Questions

[SOLVED] In Node.js, how do I "include" functions from my other files?

9 Answered Questions

[SOLVED] When should I use curly braces for ES6 import?

11 Answered Questions

[SOLVED] Ruby on Rails - Import Data from a CSV file

5 Answered Questions

[SOLVED] @import vs #import - iOS 7

1 Answered Questions

2 Answered Questions

[SOLVED] CSV file import in R

Sponsored Content