如何对数组进行循环?

时间:2022-01-18 15:40:23

I have a 1000x1 vector (1000 rows and 1 column). I want to get elements in pairs (row 1 and row 2, row 3 and row 4, row 5 and row 6, etc.)

我有一个1000x1向量(1000行和1列)我想要得到元素成对的(第1行和第2行,第3行和第4行,第5行和第6行,等等)

Here's what I have so far

这是我到目前为止所得到的

for (j in 1: ncol(total_loci)){
    for (i in 1: sample_size){
    # a pair
    genotype[i]<- paste(total_loci[i, j], total_loci[i+1,j], sep="")
    }
}

Genotype should thus be a 500x1 vector (500 rows and 1 column) containing the genotype. Assume that my for-loops are correct. I think my I needs to skip every other index -- so my i should start at 1 then 3, 5, 7, 9, etc. The variable total_loci is of class data frame.

因此,基因型应该是包含基因型的500x1向量(500行和1列)。假设for循环是正确的。我想我需要跳过所有其他的索引——我的I应该从1开始,然后3 5 7 9,等等。变量total_loci属于类数据框架。

3 个解决方案

#1


4  

Don't loop, use seq instead:

不要循环,使用seq代替:

# sample data
x <- replicate(5, sample(LETTERS, 1000, replace=TRUE), simplify=FALSE)
x <- as.data.frame(x, stringsAsFactors=FALSE)
names(x) <- paste("V",1:NCOL(x), sep="")

# function
f <- function(x) {
  s <- seq(2, length(x), 2)
  paste(x[s-1], x[s], sep="")
}

# run algorithm for each column
y <- as.data.frame(lapply(x, f), stringsAsFactors=FALSE)

#2


1  

Here is a general approach for processing an array in consecutive chunks of n elements. You can set n = 2 to process it by pairs.

这是在n个元素的连续块中处理数组的一般方法。你可以让n = 2成对处理它。

First, here is a function that splits a vector n-by-n, returning a list of n elements:

首先,这是一个分解矢量n×n的函数,返回n个元素的列表:

n.ny.n <- function(x, n) split(x, 1+(seq_along(x)-1) %% n)

n.by.n(x = 1:24, n = 2)
# $`1`
#  [1]  1  3  5  7  9 11 13 15 17 19 21 23
# 
# $`2`
#  [1]  2  4  6  8 10 12 14 16 18 20 22 24

Then you can run any function on the slices using mapply, and via do.call:

然后,您可以使用mapply和dos在切片上运行任何函数。

do.call(mapply, c(FUN = paste, n.by.n(x = 1:24, n = 2), sep = "_"))
#  [1] "1_2"   "3_4"   "5_6"   "7_8"   "9_10"  "11_12" "13_14" "15_16"
#  [9] "17_18" "19_20" "21_22" "23_24"

do.call(mapply, c(FUN = paste, n.by.n(x = 1:24, n = 6), sep = "_"))
# [1] "1_2_3_4_5_6"       "7_8_9_10_11_12"    "13_14_15_16_17_18"
# [4] "19_20_21_22_23_24"

#3


1  

Here is a way to do it without any apply family calls or loops:

这里有一种方法,无需任何应用的家庭呼叫或循环:

# Generate some sample data.
total_loci<-data.frame(genotype=sample(LETTERS,500,replace=TRUE))
# Paste
paste0(total_loci[c(TRUE,TRUE,FALSE,FALSE),],
       total_loci[c(FALSE,FALSE,TRUE,TRUE),])

#1


4  

Don't loop, use seq instead:

不要循环,使用seq代替:

# sample data
x <- replicate(5, sample(LETTERS, 1000, replace=TRUE), simplify=FALSE)
x <- as.data.frame(x, stringsAsFactors=FALSE)
names(x) <- paste("V",1:NCOL(x), sep="")

# function
f <- function(x) {
  s <- seq(2, length(x), 2)
  paste(x[s-1], x[s], sep="")
}

# run algorithm for each column
y <- as.data.frame(lapply(x, f), stringsAsFactors=FALSE)

#2


1  

Here is a general approach for processing an array in consecutive chunks of n elements. You can set n = 2 to process it by pairs.

这是在n个元素的连续块中处理数组的一般方法。你可以让n = 2成对处理它。

First, here is a function that splits a vector n-by-n, returning a list of n elements:

首先,这是一个分解矢量n×n的函数,返回n个元素的列表:

n.ny.n <- function(x, n) split(x, 1+(seq_along(x)-1) %% n)

n.by.n(x = 1:24, n = 2)
# $`1`
#  [1]  1  3  5  7  9 11 13 15 17 19 21 23
# 
# $`2`
#  [1]  2  4  6  8 10 12 14 16 18 20 22 24

Then you can run any function on the slices using mapply, and via do.call:

然后,您可以使用mapply和dos在切片上运行任何函数。

do.call(mapply, c(FUN = paste, n.by.n(x = 1:24, n = 2), sep = "_"))
#  [1] "1_2"   "3_4"   "5_6"   "7_8"   "9_10"  "11_12" "13_14" "15_16"
#  [9] "17_18" "19_20" "21_22" "23_24"

do.call(mapply, c(FUN = paste, n.by.n(x = 1:24, n = 6), sep = "_"))
# [1] "1_2_3_4_5_6"       "7_8_9_10_11_12"    "13_14_15_16_17_18"
# [4] "19_20_21_22_23_24"

#3


1  

Here is a way to do it without any apply family calls or loops:

这里有一种方法,无需任何应用的家庭呼叫或循环:

# Generate some sample data.
total_loci<-data.frame(genotype=sample(LETTERS,500,replace=TRUE))
# Paste
paste0(total_loci[c(TRUE,TRUE,FALSE,FALSE),],
       total_loci[c(FALSE,FALSE,TRUE,TRUE),])