-1

I've got a list of 2546 csv files, with different number of columns and rows each. Already imported as a list all those files as follows:

temp = list.files(pattern="*.csv")
myfiles = lapply(temp, read.csv)

As each csv have two header rows and need both info to join all csv's at the end, would like to ask you help because I was just able to do it for one of them as follows:

headers <- read.csv("filename.csv", nrows=2, header=FALSE) 

headers_names <- sapply(headers,paste,collapse="_") 

data <- read.csv(file="filename.csv", skip = 2, header=FALSE, stringsAsFactors = F)

names(data) <- headers_names

Here is an example from one of the datasets:

                   TimeDate        Hostname   VCORE  X.1.5V    AVSB   X3VCC    X.5V   X.12V
1                  TimeDate        Hostname Voltage Voltage Voltage Voltage Voltage Voltage
2 2018-02-15T12:00:45+00:00 TL-1337H1DE2018   1.728    1.56   3.312    3.36    5.16   3.072
3 2018-02-15T12:01:45+00:00 TL-1337H1DE2018   1.728    1.56   3.312    3.36    5.16   3.072
4 2018-02-15T12:02:45+00:00 TL-1337H1DE2018   1.728    1.56   3.312    3.36    5.16   3.072
5 2018-02-15T12:03:45+00:00 TL-1337H1DE2018   1.728    1.56   3.312    3.36    5.16   3.072
6 2018-02-15T12:04:45+00:00 TL-1337H1DE2018   1.728    1.56   3.312    3.36    5.16   3.072

It will need a for loop as it is a list but tried already several times without success.

Andre Elrico
  • 10,956
  • 6
  • 50
  • 69
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. It's unclear to me exactly what these files look like or how you want to join them all. – MrFlick Dec 04 '18 at 15:55
  • Sorry, tried to add the head of one datasets but seems a bit messy.. – Inês Neto Dec 04 '18 at 16:03
  • @InêsNeto does my solution work for you? – Andre Elrico Dec 04 '18 at 16:04
  • Thank you very much Andre, still trying to check if it is working – Inês Neto Dec 04 '18 at 16:14

1 Answers1

0

Wrap the one thing that works into a function fun1 that takes the filename fileN as argument.

fun1<-
function(fileN) {
    headers <- read.csv(fileN, nrows=2, header=FALSE, stringsAsFactors = FALSE) 

    headers_names <- sapply(headers,paste,collapse="_") 

    data <- read.csv(file=fileN, skip = 2, header=FALSE, stringsAsFactors = FALSE)

    names(data) <- headers_names

    return(data)
}

now apply to all filenames in temp.

ans<-
lapply(temp, fun1)
Andre Elrico
  • 10,956
  • 6
  • 50
  • 69