Skip to content
Prev 312208 / 398506 Next

data frame: adding columns from data and file title

Hello,

First of all, the best way of posting data examples is ?dput. Anyway, 
try the following.



dat <- read.table(text="
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
", header = TRUE, stringsAsFactors = FALSE)
dat
str(dat)


filename <- "G7_pig328_unit328_Site141_30MAR2012_RNo4_SitNo1.csv"

dat$cDistance <- cumsum(dat$Distance)
x <- unlist(strsplit(filename, "_"))[1:2]
x <- as.integer(sub("[[:alpha:]]+", "", x))
dat$Group <- x[1]
dat$BIRD <- x[2]
dat


Hope this helps,

Rui Barradas
Em 28-11-2012 09:33, jgui001 escreveu: