[R] Looping multiple output values to dataframe

Stropharia stevenworthington at hotmail.com
Thu Feb 12 18:46:24 CET 2009

Dear R users,

I have various vector geometry operations to perform on 3-D coordinate data
located on multiple (500+) csv files. The code I have written for the
calculations works just fine. I have written a 'for' loop to automate the
task of extracting the coordinates from the files and perform the analyses.
The loop works reasonable well, but if the number of csv files is greater
than the number of output variables the latter get repeated in the output
data frame until they equal the number of files read. I think this is
because at one stage I force the output into a matrix in order to transpose
it so that I can have variables in columns and observations in rows -
apparently this forces the matrix to be square... Is there another way to do
this that avoids the redundant columns? Also, I use the filenames (extracted
from Sys_glob) )as rownames, but this inserts the entire file path - is
there a way to just extract the actual .csv filename and use this? 

My code is below (with three, much simplified, operations). A typical
(again, much simplified) csv file looks like this:

x	y	z
10	5	2
20	15	12
30	25	22

I'm using OSX 10.5.6 and R 2.8.1. Thanks a lot for any advice.


# ----------------------------------------START
filenames <- Sys.glob("/Users/Desktop/Test/*.csv")  # get names of files to
process # use * to get all

variables <- data.frame(1:length(filenames)) # preallocate assuming multiple
values from each file # creates a dataframe with the same length of rows as
the number of .csv files to process

for (i in seq_along(filenames)){
    input <- read.csv(filenames[i], header=TRUE, na.strings="NA")  
result.A <- x[2]*y[1]
result.B <- y[2]-x[1]
result.C <- x[3]+y[1]

results <- c(result.A, result.B, result.C) # concatenate result vectors

variables[i] <- results

variables <- as.data.frame(t(as.matrix(variables))) # turn result vectors
into a matrix, then transpose it and output as a data frame

# add column and row names
c.names <- c("ResultA", "ResultB", "ResultC") # set names for result vectors
colnames(variables) <- c.names
rownames(variables) <- filenames

# export to csv file
write.csv(variables, file="/Users/Desktop/Test.csv") 
# ----------------------------------------END
View this message in context: http://www.nabble.com/Looping-multiple-output-values-to-dataframe-tp21981108p21981108.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list