[R] data frame: adding columns from data and file title

jgui001 j.guilbert at auckland.ac.nz
Wed Nov 28 10:33:18 CET 2012


Data processing”

I have a large number of csv files from animal tracks that look like this:

Date_	                              Time_		                      Speed		                     
Course	                      	Type_		                      Distance
30/03/2012	                      11:15:05		                      108		              
121	                   	                         	-2		                     
0
30/03/2012	                      11:15:06		                      0		                     
79		               	                             0		                      0
30/03/2012		                      11:15:07		                      0		                     
76	                  	                          	0		                      1
30/03/2012		                      11:15:08		                      0		                     
86		               	                             0	                      	2
30/03/2012		                      11:15:09		                      0		                     
77		                	                            0		                      3

Each file has a name like this
“G7_pig328_unit328_Site141_30MAR2012_RNo4_SitNo1.csv”

To automate the processing I would like to 
1. Add on various columns calculated from within the data frame e.g.
cumulative distance traveled (cDistance) by Summing the distance column from
[1 :n] for each row
2 Add columns derived from the file name so when I merge all the files
together I know what observation corresponds to which group and bird etc. 
For example G7 stands for group 7, pig328 is pigeon328:

The file look the same with but with these columns (plus others) added  

cDistance		                      Group	      	BIRD
0                        	                       	7		      328
0	                    	                          7		      328
1	                    	                          7		      328
3	                     	                         7		      328
6	                    	                         7		      328

I was thinking a function like this for cDistance (if I can get it to work)

cdistamce <-funtion(x){
   i = 1
   j=nrow(temp1.df)
  while(i<=j,ifelse(i=1,"Distance[i]",Sum("Distance"))
        i=i+1
}

But hit a brick wall and I have no idea about adding columns from the name.
Am I on the right track with the first one and any ideas, coz I can't brain
today I have the dumb!?

Cheers
Josh



--
View this message in context: http://r.789695.n4.nabble.com/data-frame-adding-columns-from-data-and-file-title-tp4651099.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list