[R] combining datasets by row

Rolf Turner r.turner at auckland.ac.nz
Thu Aug 30 00:19:37 CEST 2007


On 30/08/2007, at 9:41 AM, tkobayas at indiana.edu wrote:

> Rusers,
>
> I am trying to append multiple .csv files of different dimensions (but
> # of columns are the same for all .csv files). I do have .csv files
> whose names are CA1.csv ~ CA100.csv. CA means california and 1 means
> the first file. So what I have been doing (after googling how to  
> append
> by row multiple files) was:
>
> cleanup_data<-function(state,count)
> {
>    out<-matrix()
>    for (i in 1: count)
>       {
>           tmp<-read.csv(paste 
> ("CTPP3/",state,i,".csv",sep=""),header=F)
> 	  out<-rbind(out,tmp)
>       }
>    write.csv(file.out,file='file_output.csv')
> }
>
> I got an error message:
>
> cleanup_data('CA',7)
> Error in match.names(clabs, names(xi)) : names do not match  
> previous names
>
>
> Which name is this error message referring to?
>
> I decided to get out of Excel and Access trap.....

	Good thinking!

	I'd guess that the problem is with the initialization of ``out''.   
This matrix doesn't
	have *any* column names, whereas the data frame (***not*** matrix!)  
``tmp''
	will have column names V1, V2, .... (or something like that).

	You could solve your problem by initializing

		out <- NULL

	(instead of matrix()).

	Your ``write.csv'' at the end will throw an error if the function  
ever gets to it ----
	``file.out'' doesn't exist.  You perhaps want write.csv(out,  
file="etc.").

	This is not good programming anyhow --- your cleanup_data() function  
should
	*return a value*.  The write.csv call should be ``done to this  
value'', outside of
	the cleanup_data() function. IMHO.

	A sexier way to do what you want would be:

	cleanup_data <- function(state,count) {
		rslt <- list()
		for(i in 1:count) rslt[[i]] <- read.csv(paste("etc."))
		do.call(rbind,rslt)
	}

				cheers,

					Rolf Turner

######################################################################
Attention:\ This e-mail message is privileged and confidenti...{{dropped}}



More information about the R-help mailing list