I have some data files e.g 100 . and after for loop I would like to save all
data in one single data frame
file_s <- list.files(path = ".", pattern = "v2.0.2.txt",
all.files = FALSE,
full.names = FALSE, recursive = FALSE,
ignore.case = FALSE)
for (i in 1:100){
data = read.table(file_s[i],header=TRUE)
lat = data[,7] # latitude
lon = data[,8] # longitude
gas = data[,45] # gas
time.s = data[,5] # time
}
How I should get all these 100 files variable in to single data frame ?
--
View this message in context:
http://r.789695.n4.nabble.com/save-output-of-loop-tp4386599p4386599.html
Sent from the R help mailing list archive at Nabble.com.
try this:
file_s <- list.files(path = ".", pattern = "v2.0.2.txt",
all.files = FALSE,
full.names = FALSE, recursive = FALSE,
ignore.case = FALSE)
result <- do.call(rbind, lapply(file_s, function(.file){
data <- read.table(.file, header=TRUE)
data.frame(lat = data[,7] # latitude
, lon = data[,8] # longitude
, gas = data[,45] # gas
, time.s = data[,5] # time
, stringsAsFactors = FALSE
)
}))
On Tue, Feb 14, 2012 at 5:07 AM, uday <uday_143_4u at hotmail.com>
wrote:> I have some data files e.g 100 . and after for loop I would like to save
all
> data in one single data frame
>
> file_s <- list.files(path = ".", pattern =
"v2.0.2.txt", all.files = FALSE,
> ? ? ? ? ? ? ? ? ?full.names = FALSE, recursive = FALSE,
> ? ? ? ? ? ? ? ? ?ignore.case = FALSE)
> for (i in 1:100){
> ?data ? ? = read.table(file_s[i],header=TRUE)
> ?lat ?= data[,7] # latitude
> ?lon = data[,8] # longitude
> ?gas ?= data[,45] # gas
> ?time.s ? = data[,5] # time
> }
>
> How I should get all these 100 files variable in to single data frame ?
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/save-output-of-loop-tp4386599p4386599.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
The short answer to your question is *don't* concatenate the values in the
row, then attempt to /rbind()/ them incrementally to a data.frame. Instead
build each column separately inside the loop, then /cbind() (data.frame()/
does an implicit/ cbind()/ ) them together at the end. Something like this:
/
lat.column <- c(length(100))
lon.column <- c(length(100))
...
for (i in 1:100){
...
lat.column[i] <- data[,7] # latitude
lon.column[i] <- data[,8] # longitude
...
}
my.data <- data.frame(lat.column, lon.column, ...)
/
-jm
--
View this message in context:
http://r.789695.n4.nabble.com/save-output-of-loop-tp4386599p4387476.html
Sent from the R help mailing list archive at Nabble.com.
The dimensions of variables are unknown , they changes to every file. -- View this message in context: http://r.789695.n4.nabble.com/save-output-of-loop-tp4386599p4387804.html Sent from the R help mailing list archive at Nabble.com.