Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
程序R:如何创建循环以基于级别处理部分数据集?_R_Loops_Batch Processing - Fatal编程技术网

程序R:如何创建循环以基于级别处理部分数据集?

程序R:如何创建循环以基于级别处理部分数据集?,r,loops,batch-processing,R,Loops,Batch Processing,我的数据集如下所示: 数据我偏爱拆分应用方法来解决这类问题,尽管正如mnel指出的那样,还有其他选择 我会将矩阵转化为一个函数,然后按组分割数据,并将该函数应用于每个组,如下所示: #your data renamed dat (data is an R function so avoid using that as a name dat <- structure(list(group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,

我的数据集如下所示:


数据我偏爱
拆分
应用
方法来解决这类问题,尽管正如mnel指出的那样,还有其他选择

我会将矩阵转化为一个函数,然后按组分割数据,并将该函数应用于每个组,如下所示:

#your data renamed dat (data is an R function so avoid using that as a name
dat <- structure(list(group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("I", "II", "III"), class = "factor"), 
    time = c(1L, 1L, 1L, 2L, 2L, 3L, 1L, 1L, 1L, 2L, 2L, 1L, 
    2L, 2L, 2L), species = structure(c(1L, 2L, 3L, 2L, 4L, 1L, 
    3L, 2L, 1L, 3L, 4L, 1L, 1L, 3L, 4L), .Label = c("a", "b", 
    "c", "d"), class = "factor")), .Names = c("group", "time", 
"species"), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"
))

#your processing turned into a function
FUN <- function(DATA) {
    X <- table(DATA[, 2],DATA[, 1])    
    X <- as.matrix(X)    
    X %*% t(X)
}

#the split lapply method
X <- split(dat[, 2:3], dat[, 1])    
lapply(X, FUN)
编辑:我很抱歉,我错过了你想把每一个都写进一个文件的机会。我这样做了,但是您可能想考虑上面函数的输出,而不是编写多个TXT文件:<代码>保存> <代码>或<代码> SAVDSDS <代码>函数:

v <- split(dat[, 2:3], dat[, 1])    
Output <- lapply(seq_along(v), function(i) {
        X <- table(v[[i]][, 2], v[[i]][, 1])    
        X <- as.matrix(X)    
        z <- X %*% t(X)
        write.table(z, paste0("coocurrence_group", names(v)[i], ".txt"),sep="\t")
        return(z)
    }
)

names(Output) <- names(v)
Output
v查看or或
split
lappy
#your data renamed dat (data is an R function so avoid using that as a name
dat <- structure(list(group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("I", "II", "III"), class = "factor"), 
    time = c(1L, 1L, 1L, 2L, 2L, 3L, 1L, 1L, 1L, 2L, 2L, 1L, 
    2L, 2L, 2L), species = structure(c(1L, 2L, 3L, 2L, 4L, 1L, 
    3L, 2L, 1L, 3L, 4L, 1L, 1L, 3L, 4L), .Label = c("a", "b", 
    "c", "d"), class = "factor")), .Names = c("group", "time", 
"species"), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"
))

#your processing turned into a function
FUN <- function(DATA) {
    X <- table(DATA[, 2],DATA[, 1])    
    X <- as.matrix(X)    
    X %*% t(X)
}

#the split lapply method
X <- split(dat[, 2:3], dat[, 1])    
lapply(X, FUN)
$I

    a b c d
  a 2 1 1 0
  b 1 2 1 1
  c 1 1 1 0
  d 0 1 0 1

$II

    a b c d
  a 1 1 1 0
  b 1 1 1 0
  c 1 1 2 1
  d 0 0 1 1

$III

    a b c d
  a 2 0 1 1
  b 0 0 0 0
  c 1 0 1 1
  d 1 0 1 1
v <- split(dat[, 2:3], dat[, 1])    
Output <- lapply(seq_along(v), function(i) {
        X <- table(v[[i]][, 2], v[[i]][, 1])    
        X <- as.matrix(X)    
        z <- X %*% t(X)
        write.table(z, paste0("coocurrence_group", names(v)[i], ".txt"),sep="\t")
        return(z)
    }
)

names(Output) <- names(v)
Output