a p
a p

Reputation: 1

why does rbind need a loop to create a data frame and not just a vector of matrixes

for (i in 1:5) {
        dat <- rbind(dat, read.csv(files_full[i]))

works, but

dat <- rbind(dat, read.csv(files_full[1:5]))

doesn't:

Error in file(file, "rt") : invalid 'description' argument

files_full returns this:

[1] "diet_data/Andy.csv"  "diet_data/David.csv" "diet_data/John.csv" 
[4] "diet_data/Mike.csv"  "diet_data/Steve.csv"

from this exercise: https://github.com/rdpeng/practice_assignment/blob/master/practice_assignment.rmd

Upvotes: 0

Views: 62

Answers (1)

MrFlick
MrFlick

Reputation: 206401

rbind() is meant to bind all it's parameters, not elements contained in lists inside of its parameters. For example

dat <- rbind(read.csv(files_full[1]), read.csv(files_full[2], read.csv(files_full[3])

would work. If you want to turn a list into parameter, you use do.call

dat <- do.call("rbind", Vectorize(read.csv, SIMPLIFY = FALSE)(files_full))

Here I used Vectorize() to allow read.csv to return a list when given a vector of file names.

Upvotes: 1

Related Questions