[R] Reading multiple text files and then combining into a dataframe
jim holtman
jholtman at gmail.com
Sat Dec 3 13:22:42 CET 2011
Does this do it for you:
> #Creating example data in similar format to data I have
> sub <- rep(1,10)
> trial <- seq(1,10,1)
> size <- rep(3,10)
> shortest <- rep(444,10)
> startlab <- rep(444,10)
> endlab <- rep(444,10)
> startconf <- rep(444,10)
> endconf <- rep(444,10)
> steps <- sample(1:30,10)
> time <- sample(1:300,10)
>
> #creating example of text file and saving into a shared director with other txt files
>
> subject1 <-
+ cbind(sub,trial,size,shortest,startlab,endlab,startconf,endconf,steps,time)
> write.table(subject1, "/temp/subject1.txt", sep=" ")
>
> #2nd example of same text file
> sub <- rep(2,10)
> trial <- seq(1,10,1)
> size <- rep(3,10)
> shortest <- rep(444,10)
> startlab <- rep(444,10)
> endlab <- rep(444,10)
> startconf <- rep(444,10)
> endconf <- rep(444,10)
> steps <- sample(1:30,10)
> time <- sample(1:300,10)
>
> subject1 <-
+ cbind(sub,trial,size,shortest,startlab,endlab,startconf,endconf,steps,time)
> write.table(subject1, "/temp/subject2.txt", sep=" ")
>
>
> setwd("/temp")
>
> #Getting list of file names
> file_name <- list.files(pattern="subject*")
>
> mydata <- lapply (file_name, read.table, sep=" ", header=T, row.names=NULL)
>
> # rbind to dataframe
> mydf <- do.call(rbind, mydata)
>
> # or use the plyr package
> require(plyr)
> mydf.1 <- ldply(file_name, read.table, sep = ' ', header = TRUE, row.names = NULL)
>
> str(mydf)
'data.frame': 20 obs. of 11 variables:
$ row.names: chr "1" "2" "3" "4" ...
$ sub : int 1 1 1 1 1 1 1 1 1 1 ...
$ trial : int 1 2 3 4 5 6 7 8 9 10 ...
$ size : int 3 3 3 3 3 3 3 3 3 3 ...
$ shortest : int 444 444 444 444 444 444 444 444 444 444 ...
$ startlab : int 444 444 444 444 444 444 444 444 444 444 ...
$ endlab : int 444 444 444 444 444 444 444 444 444 444 ...
$ startconf: int 444 444 444 444 444 444 444 444 444 444 ...
$ endconf : int 444 444 444 444 444 444 444 444 444 444 ...
$ steps : int 10 28 20 18 4 21 5 3 8 7 ...
$ time : int 225 188 205 189 103 227 148 221 275 14 ...
> str(mydf.1)
'data.frame': 20 obs. of 11 variables:
$ row.names: chr "1" "2" "3" "4" ...
$ sub : int 1 1 1 1 1 1 1 1 1 1 ...
$ trial : int 1 2 3 4 5 6 7 8 9 10 ...
$ size : int 3 3 3 3 3 3 3 3 3 3 ...
$ shortest : int 444 444 444 444 444 444 444 444 444 444 ...
$ startlab : int 444 444 444 444 444 444 444 444 444 444 ...
$ endlab : int 444 444 444 444 444 444 444 444 444 444 ...
$ startconf: int 444 444 444 444 444 444 444 444 444 444 ...
$ endconf : int 444 444 444 444 444 444 444 444 444 444 ...
$ steps : int 10 28 20 18 4 21 5 3 8 7 ...
$ time : int 225 188 205 189 103 227 148 221 275 14 ...
>
On Fri, Dec 2, 2011 at 11:51 PM, James Holland <holland.aggie at gmail.com> wrote:
> I have a multiple text files, separated by a single space, that I need to
> combine into a single data.frame. Below is the track I'm on using
> list.files to capture the names of the files and then lapply with
> read.table. But I run into problems making a usable dataframe out of the
> data.
>
>
> #Creating example data in similar format to data I have
> sub <- rep(1,10)
> trial <- seq(1,10,1)
> size <- rep(3,10)
> shortest <- rep(444,10)
> startlab <- rep(444,10)
> endlab <- rep(444,10)
> startconf <- rep(444,10)
> endconf <- rep(444,10)
> steps <- sample(1:30,10)
> time <- sample(1:300,10)
>
> #creating example of text file and saving into a shared director with other
> txt files
>
> subject1 <-
> cbind(sub1,trial,size,shortest,startlab,endlab,startconf,endconf,steps,time)
> write.table(subject1, "C:Folder/R/subject1.txt", sep=" ")
>
> #2nd example of same text file
> sub <- rep(2,10)
> trial <- seq(1,10,1)
> size <- rep(3,10)
> shortest <- rep(444,10)
> startlab <- rep(444,10)
> endlab <- rep(444,10)
> startconf <- rep(444,10)
> endconf <- rep(444,10)
> steps <- sample(1:30,10)
> time <- sample(1:300,10)
>
> subject1 <-
> cbind(sub1,trial,size,shortest,startlab,endlab,startconf,endconf,steps,time)
> write.table(subject1, "C:Folder/R/subject2.txt", sep=" ")
>
>
> setwd("C:Folder/R/")
>
> #Getting list of file names
> file_name <- list.files(pattern="subject*")
>
> mydata <- lapply (file_name, read.table, sep=" ", header=T, row.names=NULL)
>
>
>
> Thank you,
> James
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
More information about the R-help
mailing list