-1

I have a large comma delimted file that looks something like this:

LS_trap_10c,7C000000395C1641,trap10c_7C000000395C1641_150809.csv,c,5/30/2015,1800,25
LS_trap_10c,7C000000395C1641,trap10c_7C000000395C1641_150809.csv,c,5/30/2015,2000,24.5
LS_trap_10c,7C000000395C1641,trap10c_7C000000395C1641_150809.csv,c,5/30/2015,2200,24.5
LS_trap_10c,7C000000395C1641,trap10c_7C000000395C1641_150809.csv,c,5/31/2015,000,24
LS_trap_10c,7C000000395C1641,trap10c_7C000000395C1641_150809.csv,c,5/31/2015,200,23.5
LS_trap_10c,7C000000395C1641,trap10c_7C000000395C1641_150809.csv,c,5/31/2015,400,23.5,97
LS_trap_10c,7C000000395C1641,trap10c_7C000000395C1641_150809.csv,c,5/31/2015,600,23.5,98.5

As you can see the data vary (the bottom two instances have an extra column) and not all columns contain values. This data displays correctly in excel, but when I attempt to open it in RStudio with:

my_trap_dat = read.csv("path_to_file/la_selva_log.csv",(header = FALSE))

It does not contain all of the data- it leaves out the last column- so i have 7 columns instead of the 8 that are needed to display all data. The data in the last column seem to be just removed from the set when you load them into R.

I found this:

The number of data columns is determined by looking at the first five lines of input (or the whole input if it has less than five lines), or from the length of col.names if it is specified and is longer.

But I'm not sure how to implement any change that fixes my issue.

How can I make it so that all of my data is maintained in R?

Mark Fitzgerald
  • 3,048
  • 3
  • 24
  • 29
5r9n
  • 177
  • 2
  • 11
  • Give your data headers for each column, then set header to true. Alternatively sort your data such that the rows with the most number of columns are ontop. – Jean Mar 27 '17 at 05:04
  • 2
    I'm guessing that came from a lab instrument so in addition to the answers given, consider (1) looking to see if the instrument software has any configuration options for CSV export and (2) letting the vendor know that their exported CSV is invalid. – neilfws Mar 27 '17 at 05:13
  • Thanks for the tip- I don't actually have access to the equipment itself, only to archival data files that we're trying to make sense of. And their exported csv is atrocious, for many reasons other than this. – 5r9n Mar 27 '17 at 05:18

1 Answers1

0

This question is answered already on StackOverflow:

  1. How can you read a CSV file in R with different number of columns
  2. Read a text file with variable number of columns to a list

I'm sure you find more on stack overflow using the Search.

Quick example (given your exported CSV is not valid):

my_file = file("path_to_file/la_selva_log.csv")
my_data = strsplit(readLines(my_file), ",")
close(my_file)
Community
  • 1
  • 1
stephanmg
  • 746
  • 6
  • 17