简体   繁体   English

R嵌套的循环合并数据帧

[英]R nested for loops merge data frames

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. 我有一个数据框,其中每一列都是一个数字时间序列(从0到8),代表动物求爱期间的不同行为。 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: 因此,我运行该函数以说出第三列,行为3和时间上的下一个行为(1),我得到了我想要的:

> 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 预先感谢,何塞

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)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM