Reputation: 115
I have a data frame in which each column is a time series of numbers (from 0 to 8) representing different behaviors during animal courtship. I would like to check if there is a pattern such as a given behavior is followed more frequently by another one. I have written a function that allow me to do calculate the frequencies of behaviours that follow a given behavior after a particular time interval:
> data[,3]
[1] 1 1 1 1 7 7 3 3 7 3 1 1 8 1 3 3 3 5 1 1 4 ...
neighbor <- function(DATA, BEHAVIOR, INTERVAL)
{
total=c(0)
tmp = data.frame(total=c(0:8),Freq=rep(0,9))
number_of_x = which(DATA == BEHAVIOR)
for(i in number_of_x){
total = append(total,DATA[i+INTERVAL,])
}
tmp = merge(tmp,table(total), by=c("total"), all=T)
tmp[is.na(tmp)] <- 0
subset(tmp, select = ncol(tmp))
}
So I run the function for say the third column, behavior 3, and next behavior in time (1) and I get what I want:
> neighbor(as.data.frame(data[,3], 3, 1]
Freq.y
0 0.01
1 0.71
2 0.01
3 0.21
4 0.01
5 0.04
6 0.01
7 0.02
8 0.00
Now I would like to use a similar function to obtain the frequencies for the nine behaviours. Something like:
neighborAll <- function(DATA, INTERVAL)
{
total=c(0)
tmp = data.frame(total=c(0:8),Freq=rep(0,9))
for(a in c(0:8)){
number_of_x = which(DATA == a)
for(i in number_of_x){
total = c(total,DATA[i+INTERVAL,])
}
tmp=merge(tmp, table(total), by = c("total"), all=T)
tmp[is.na(tmp)] <- 0
}
tmp[,3:9]
}
> neighborAll(as.data.frame(data[,3], 1)
I get:
Error in merge.data.frame(tmp, table(total), by = c("total"), all = T) :
there is already a column named ‘Freq.x’
Any ideas would be welcomed. Thanks in advance, Jose
Upvotes: 1
Views: 1514
Reputation: 37744
If it weren't for getting the names right, each of these could be a one-liner.
neighbor <- function(DATA, BEHAVIOR, INTERVAL) {
nbins <- 1+max(0, DATA, na.rm = TRUE)
out <- tabulate(1+DATA[which(DATA==BEHAVIOR)+INTERVAL], nbins=nbins)
names(out) <- 1:nbins - 1
out
}
neighborAll <- function(DATA, INTERVAL) {
out <- sapply(0:max(DATA, na.rm=TRUE),
function(BEHAVIOR) neighbor(DATA, BEHAVIOR, INTERVAL))
colnames(out) <- 0:max(DATA, na.rm=TRUE)
out
}
> x <- c(1, 1, 1, 1, 7, 7, 3, 3, 7, 3, 1, 1 ,8, 1, 3, 3, 3, 5, 1, 1, 4)
> neighbor(x,3,1)
0 1 2 3 4 5 6 7 8
0 1 0 3 0 1 0 1 0
> neighborAll(x,1)
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 5 0 1 0 1 0 0 1
2 0 0 0 0 0 0 0 0 0
3 0 1 0 3 0 0 0 2 0
4 0 1 0 0 0 0 0 0 0
5 0 0 0 1 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0
7 0 1 0 1 0 0 0 1 0
8 0 1 0 0 0 0 0 0 0
Upvotes: 0
Reputation: 4643
Essentially you want this:
neighborAll <- function(DATA, INTERVAL, TABLE)
{
for(i in 1:(nrow(TABLE) - 1))
{
neighbors <- DATA[which(DATA == i) + INTERVAL]
tab <- table(neighbors)
TABLE[TABLE$behavior %in% names(tab), i + 2] <- tab
}
return(TABLE)
}
x<-c(1, 1, 1, 1, 7, 7, 3, 3, 7, 3, 1, 1 ,8, 1, 3, 3, 3, 5, 1, 1, 4)
behavior <- 0:8
n <- length(behavior)
tmp <- matrix(nrow=n, ncol=n)
colnames(tmp) <- paste("freq", behavior, sep="")
freqtab <- data.frame(behavior, tmp)
neighborAll(x, 1, freqtab)
Upvotes: 1